tsql - How to get last part of nvarchar with variable size in T-SQL? -


imagine have following value in nvarchar variable:

declare @txt nvarchar(255) set @txt = '32|foo|foo2|123' 

is there way last part after last | 123 in case ?

i write split function i'm not interested in first parts of string. there way last part of string without getting first parts ?

note parts of string have variable sizes.

you can use combination of left, reverse , charindex this. query below reverses string, finds first occurance of |, strips out other characters , straightens string back.

declare @txt nvarchar(255) set @txt = '32|foo|foo2|123'  select reverse(left(reverse(@txt),charindex('|',reverse(@txt))-1)) 

output

123 

edit

if string has 4 parts or less , . isn't valid character, can use parsename this.

declare @txt nvarchar(255) set @txt = '32|foo|foo2|123' select parsename(replace(@txt,'|','.'),1) 

Comments