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?
In case the start_position is positive, the SUBSTR() function will count from the beginning of the str to determine the first character of the substring. If the start_position is negative, then the SUBSTR() function will count backward from the end of the str to find the first character of the substring.
The INSTR functions search string for substring . The function returns an integer indicating the position of the character in string that is the first character of this occurrence.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With