How to get first two characters of a string in oracle query?
SqlOracleSql Problem Overview
Suppose I have a column name OrderNo
with value AO025631
in a table shipment
.
I am trying to query the table so that I can get only first two character of column value i.e. AO
.
Can I do this in the SQL query itself?
Sql Solutions
Solution 1 - Sql
SUBSTR (documentation):
SELECT SUBSTR(OrderNo, 1, 2) As NewColumnName from shipment
When selected, it's like any other column. You should give it a name (with As
keyword), and you can selected other columns in the same statement:
SELECT SUBSTR(OrderNo, 1, 2) As NewColumnName, column2, ... from shipment
Solution 2 - Sql
select substr(orderno,1,2) from shipment;
You may want to have a look at the documentation too.
Solution 3 - Sql
take a look here
SELECT SUBSTR('Take the first four characters', 1, 4) FIRST_FOUR FROM DUAL;
Solution 4 - Sql
Easy:
SELECT SUBSTR(OrderNo, 1, 2) FROM shipment;
Solution 5 - Sql
Just use SUBSTR function. It takes 3 parameters: String column name, starting index and length of substring:
select SUBSTR(OrderNo, 1, 2) FROM shipment;
Solution 6 - Sql
Try select substr(orderno, 1,2) from shipment;