sql server - substring in sqlserver with casual format -
i have these rows
14-l-100-10008-g03s-n 1-dr-100-20057-g48-n 2-cl-100-12133-b12-p 2-fg-100-12202-b01-n 2-g-100-15520-b48-n 2-gm-100-10024-b01-n 2-hc-100-10022-g03s-n 2-hc-100-10023-g03s-n 2-hc-100-20023-g03s-n 32-g-100-15518-f03p2-n 32-g-100-15518-f03p2-n
i need these parts .how can these parts using sql server code :
result:g03s,g48,b12,b01,b48,b12 , ...
you can below:
;with cte ( select '14-l-100-10008-g03s-n' val union select '1-dr-100-20057-g48-n' val union select '2-cl-100-12133-b12-p' val union select '2-fg-100-12202-b01-n' val union select '2-g-100-15520-b48-n' val union select '2-gm-100-10024-b01-n' val union select '2-hc-100-10022-g03s-n' val union select '2-hc-100-10023-g03s-n' val union select '2-hc-100-20023-g03s-n' val union select '32-g-100-15518-f03p2-n' val union select '32-g-100-15518-f03p2-n' ) select reverse(substring(reverse(val), charindex('-', reverse(val), 0) + 1, charindex('-', reverse(val), charindex('-', reverse(val), 0) + 1) - charindex('-', reverse(val)) - 1)) cte
edit:
select ( select reverse(substring(reverse(val), charindex('-', reverse(val), 0) + 1, charindex('-', reverse(val), charindex('-', reverse(val), 0) + 1) - charindex('-', reverse(val)) - 1)) + ' ,' cte xml path ('') ) result
result: g03s ,g48 ,b12 ,b01 ,b48 ,b01 ,g03s ,g03s ,g03s ,f03p2 ,f03p2 ,
select left(a.result, len(a.result) -1) (select (select reverse(substring(reverse(val), charindex('-', reverse(val), 0) + 1, charindex('-', reverse(val), charindex('-', reverse(val), 0) + 1) - charindex('-', reverse(val)) - 1)) + ' ,' cte xml path ('') ) result)
result: g03s ,g48 ,b12 ,b01 ,b48 ,b01 ,g03s ,g03s ,g03s ,f03p2 ,f03p2
Comments
Post a Comment