Tuesday, February 14, 2012

CSV

Hi,

I have a variable like @.states=NY,VA,AL,CA.

Now i want to use the above string in the IN clause.How to separate those string and use.

Select * from xxx where @.State IN('NY','VA','AL','CA')

Thanks in advance

Create Function dbo.ListOfvalues(@.values varchar(8000),@.delimeter char) Returns @.list table (Item varchar(1000))
as
Begin
While CharIndex(@.delimeter,@.values) >0
Begin
Insert Into @.list
Select Ltrim(RTrim(Left(@.values,CharIndex(@.delimeter,@.values)-1)))
Select @.values=RTRIM(Ltrim(Right(@.values,len(@.values)-CharIndex(@.delimeter,@.values))))
End
Insert Into @.list
Select @.values
Return;
End

go

Set @.States = 'NY,VA,AL,CA'

Select * from xxx where State IN (Select item from dbo.ListOfvalues(@.States,','))

|||

Thank you very much.

No comments:

Post a Comment