This question has a database specific answer.
If using SQL Server:
SELECT column1
, RIGHT(column2,CHARINDEX('-',REVERSE(column2))-1) as extracted
, column3
FROM myTable
You can add a CASE
statement or use NULLIF()
in case the hyphen isn't always present:
SELECT column1
, CASE WHEN column2 LIKE '%-%' THEN RIGHT(column2,CHARINDEX('-',REVERSE(column2))-1)
END as extracted
, column3
FROM myTable
Or:
SELECT column1
, RIGHT(column2,NULLIF(CHARINDEX('-',REVERSE(column2)),0)-1) as extracted
, column3
FROM myTable
If using MySQL just change CHARINDEX()
to LOCATE()
. I believe Oracle it's INSTR()
and the first two parameters are switched, first it's the string you're searching in, then the string you're searching for.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…