| Author |
Topic  |
|
|
razeena
Starting Member
47 Posts |
Posted - 12/18/2012 : 02:14:35
|
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
1390 Posts |
Posted - 12/18/2012 : 02:17:34
|
Use this condition
WHERE ',' + @l1 + ',' LIKE '%,' + CAST(Col3 AS varchar) + ',%'
-- Chandu |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
USA
6997 Posts |
Posted - 12/18/2012 : 11:00:20
|
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 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
India
47023 Posts |
Posted - 12/19/2012 : 01:00:09
|
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/
|
 |
|
|
bandi
Flowing Fount of Yak Knowledge
India
1390 Posts |
Posted - 12/19/2012 : 01:04:45
|
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 |
 |
|
| |
Topic  |
|