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

Time:12-06

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

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

but I get values before / and not after /

CodePudding user response:

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

CodePudding user response:

additional way:

declare @string varchar(50) = 'NA/ANN';

select parsename(replace(@string,'/','.'),1)
  • Related