Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 716
coblenski Avatar asked Aug 16 '16 11:08

coblenski


People also ask

How do I get the string before a character in SQL Server?

Use the SUBSTRING() function. The first argument is the string or the column name. The second argument is the index of the character at which the substring should begin. The third argument is the length of the substring.


2 Answers

You can use:

select right(col, charindex('-', reverse(col)) - 1) 
like image 162
Gordon Linoff Avatar answered Sep 24 '22 06:09

Gordon Linoff


DECLARE @x varchar(100) SET @x = 'abcd-efgh-XXXX' SELECT RIGHT(@x, CHARINDEX('-', REVERSE(@x)) - 1) 
like image 40
ADyson Avatar answered Sep 25 '22 06:09

ADyson