Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get string after character oracle

I have VP3 - Art & Design and HS5 - Health & Social Care, I need to get string after '-' in Oracle. Can this be achieved using substring?

like image 487
Aruna Raghunam Avatar asked Jul 18 '17 11:07

Aruna Raghunam


People also ask

How do you find a specific character in a string in Oracle?

The Oracle INSTR function is used to search string for substring and find the location of the substring in the string. If a substring that is equal to substring is found, then the function returns an integer indicating the position of the first character of this substring.

What is CHR 32 in Oracle?

If the column is of CHAR(n) datatype and the string you have entered does not have n characters then Oracle will pad the data with space ( CHR(32) ) characters until it has exactly n characters.

How do I remove a character from a string in Oracle?

The Oracle LTRIM() function is used to remove all specified characters from the left end side of a string. Optionally you can specify an initial character or characters to trim to, or it will default to a blank. The string to trim the characters from the left-hand side.


2 Answers

For a string operation as simple as this, I might just use the base INSTR() and SUBSTR() functions. In the query below, we take the substring of your column beginning at two positions after the hyphen.

SELECT
    SUBSTR(col, INSTR(col, '-') + 2) AS subject
FROM yourTable

We could also use REGEXP_SUBSTR() here (see Gordon's answer), but it would be a bit more complex and the performance might not be as good as the above query.

like image 179
Tim Biegeleisen Avatar answered Oct 08 '22 21:10

Tim Biegeleisen


You can use regexp_substr():

select regexp_substr(col, '[^-]+', 1, 2)

If you want to remove an optional space, then you can use trim():

select trim(leading ' ', regexp_substr(col, '[^-]+', 1, 2))

The non-ovious parameters mean

  • 1 -- search from the first character of the source. 1 is the default, but you have to set it anyway to be able to provide the second parameter.
  • 2 -- take the second match as the result substring. the default would be 1.
like image 26
Gordon Linoff Avatar answered Oct 08 '22 21:10

Gordon Linoff