0

I have a varchar variable been defined like this.

declare @IDs varchar(50)
set  @IDs ='111,123,567,'

Now I need to extract the last value in the list always 567.

The values in @IDs can be like this also

set  @IDs ='56,'

In this case we need extract only the value 56.

How can we do it?

halfer
  • 19,824
  • 17
  • 99
  • 186
happysmile
  • 7,537
  • 36
  • 105
  • 181
  • Something similar has already been asked: http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-reco – David Brabant Feb 21 '12 at 06:44

2 Answers2

1

i think you will find this user defined function to split the string helpful:

http://www.codeproject.com/Articles/7938/SQL-User-Defined-Function-to-Parse-a-Delimited-Str

Vikram
  • 8,235
  • 33
  • 47
0

You can use the string splitter found here: http://www.sqlservercentral.com/articles/Tally+Table/72993/

It is very fast, you can call it like so:

SELECT *
FROM dbo.DelimitedSplit8K(@IDs,',')

This will return you a result set of all the values in the string.

Vince Pergolizzi
  • 6,396
  • 3
  • 19
  • 16