happysmile
happysmile

Reputation: 7777

Split values in an variable in SQL Server 2005

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?

Upvotes: 0

Views: 611

Answers (2)

Vince Pergolizzi
Vince Pergolizzi

Reputation: 6584

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.

Upvotes: 0

Vikram
Vikram

Reputation: 8333

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

Upvotes: 1

Related Questions