SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Comma separated integer value in query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

razeena
Yak Posting Veteran

53 Posts

Posted - 12/18/2012 :  02:14:35  Show Profile  Reply with Quote
Hi,
-------------
Declare @l1 varchar(10)= '4,5'
Select
Col1,
Col2
From
<tableName>
Where
Col3 in (@l1)
-----
Col3 is an integer value
This is showing error as "Conversion failed when converting the varchar value '4,5' to data type int."
What I needed was
---
Select
Col1,
Col2
From
<tableName>
Where
Col3 in (4,5)
--------
Any ideas about how to pass the argument?

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 12/18/2012 :  02:17:34  Show Profile  Reply with Quote
Use this condition

WHERE ',' + @l1 + ',' LIKE '%,' + CAST(Col3 AS varchar) + ',%'

--
Chandu
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 12/18/2012 :  11:00:20  Show Profile  Reply with Quote
quote:
Originally posted by razeena

Hi,
-------------
Declare @l1 varchar(10)= '4,5'
Select
Col1,
Col2
From
<tableName>
Where
Col3 in (@l1)
-----
Col3 is an integer value
This is showing error as "Conversion failed when converting the varchar value '4,5' to data type int."
What I needed was
---
Select
Col1,
Col2
From
<tableName>
Where
Col3 in (4,5)
--------
Any ideas about how to pass the argument?



You can use a split function to parse the comma delimited list into a temp table and then use that in your query.

The function on the link below should do the job.
Tally OH! An Improved SQL 8K “CSV Splitter” Function
http://www.sqlservercentral.com/articles/Tally+Table/72993/





CODO ERGO SUM
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/19/2012 :  01:00:09  Show Profile  Reply with Quote
quote:
Originally posted by bandi

Use this condition

WHERE ',' + @l1 + ',' LIKE '%,' + CAST(Col3 AS varchar) + ',%'

--
Chandu


can cause performance hit in case of large datasets. Also in case Col3 contain spaces or other characters that has to be handled as well

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 12/19/2012 :  01:04:45  Show Profile  Reply with Quote
quote:
Originally posted by visakh16
WHERE ',' + @l1 + ',' LIKE '%,' + CAST(Col3 AS varchar) + ',%'
can cause performance hit in case of large datasets. Also in case Col3 contain spaces or other characters that has to be handled as well


hmm. Thanks for your suggestion visakh

--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000