How to get first two characters of a string in oracle query?
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?
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
select substr(orderno,1,2) from shipment;
You may want to have a look at the documentation too.
take a look here
SELECT SUBSTR('Take the first four characters', 1, 4) FIRST_FOUR FROM DUAL;