How to get first two characters of a string in oracle query?

SqlOracle

Sql 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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVivekView Question on Stackoverflow
Solution 1 - SqlmanjiView Answer on Stackoverflow
Solution 2 - SqlTony AndrewsView Answer on Stackoverflow
Solution 3 - SqlthomasView Answer on Stackoverflow
Solution 4 - SqlDatajamView Answer on Stackoverflow
Solution 5 - SqlFarshid ZakerView Answer on Stackoverflow
Solution 6 - SqlShepherdessView Answer on Stackoverflow