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