Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

oracle

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?

like image 335
Vivek Avatar asked Jun 03 '11 09:06

Vivek


People also ask

How do you get the first character of a string in PL SQL?

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.

How do you find the first occurrence of a character in a string in Oracle?

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.


1 Answers

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 
like image 89
manji Avatar answered Oct 08 '22 19:10

manji