[sql] 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?

This question is related to sql oracle

The answer is


Try select substr(orderno, 1,2) from shipment;


Just use SUBSTR function. It takes 3 parameters: String column name, starting index and length of substring:

select SUBSTR(OrderNo, 1, 2) FROM shipment;

take a look here

SELECT SUBSTR('Take the first four characters', 1, 4) FIRST_FOUR FROM DUAL;

select substr(orderno,1,2) from shipment;

You may want to have a look at the documentation too.


Easy:

SELECT SUBSTR(OrderNo, 1, 2) FROM shipment;