Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tsql remove all values left of char / , if char is present in a string

I have a column with possible values as "NA/ANN" or "system"
Now I want to get the contents of this string after /, if / is present.
If / is not present the string itself.

Result as "ANN" and "system"

I tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select LEFT(LogUser, CHARINDEX('\', LogUser)) from table1 

but I get values before / and not after /

>Solution :

One of many ways you can do this is as follows using a combination of standard string functions right and charindex; Nulif is used to handle when there are no delimiters at all.

declare @string varchar(50) = '123/xxyyy';

select Right(@string, IsNull(NullIf(CharIndex('/',Reverse(@string)),0)-1, Len(@string)));

Example DB Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading