'SQL Select everything after character
I'd like to select everything AFTER a certain character (-) that is placed on the most right side.
Eg.
abcd-efgh-XXXX
And I'd like to select the XXXX
part
Thanks!
Solution 1:[1]
You can use:
select right(col, charindex('-', reverse(col)) - 1)
Solution 2:[2]
DECLARE @x varchar(100)
SET @x = 'abcd-efgh-XXXX'
SELECT RIGHT(@x, CHARINDEX('-', REVERSE(@x)) - 1)
Solution 3:[3]
Using string split available from SQLServer 2016
;with cte
as
(
select
*,row_number() over (order by (select null)) as rownum
from string_split('abcd-efgh-XXXX','-')
)
select top 1 * from cte
order by rownum desc
Solution 4:[4]
@thegameiswar had a clever solution, since I needed the results from a comma delimited list. I don't have SQL 2016, so I made it work with a user defined split function.
;with cte
as
(
select
*,row_number() over (order by (select null)) as rownum
from database..[fn_SplitDelimitedList](@CommaDelimitedList,',')
)
select * from cte
order by rownum desc
Solution 5:[5]
select substr('Prueba,Prueba2',instr('Prueba,Prueba2',',') + 1) from dual
Solution 6:[6]
SQL Server Management Studio v15.0.18206.0 (18.4):
RIGHT([col], CHARINDEX('-', REVERSE([col]), -1))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gordon Linoff |
Solution 2 | ADyson |
Solution 3 | |
Solution 4 | |
Solution 5 | 4b0 |
Solution 6 | Ajay2707 |