SQL Conversion Issue

Multi tool use
up vote
0
down vote
favorite
I have a temp table where i am creating all the values of column to a comma separated list.. the issue i am facing is when i am inputting that csv variable i am getting error
For ex
Declare @testvar varchar (max)
Select @testvar = stuff (( select (','''+ cast(var1 as varchar(1024)+'''')
From # Temp for xmlpath('')),1,2,'') from #Temp
Select * from tab1 where col1 in (@testvar)
This is appending single quotes in query i am getting error like
Conversion failed while converting varchar value ''1','2'........ to int
Any suggestions for this
sql

|
show 2 more comments
up vote
0
down vote
favorite
I have a temp table where i am creating all the values of column to a comma separated list.. the issue i am facing is when i am inputting that csv variable i am getting error
For ex
Declare @testvar varchar (max)
Select @testvar = stuff (( select (','''+ cast(var1 as varchar(1024)+'''')
From # Temp for xmlpath('')),1,2,'') from #Temp
Select * from tab1 where col1 in (@testvar)
This is appending single quotes in query i am getting error like
Conversion failed while converting varchar value ''1','2'........ to int
Any suggestions for this
sql

Which dbms are you using?
– jarlh
Nov 8 at 8:21
2
# Temp
would be a wrong syntax
– SchmitzIT
Nov 8 at 8:23
You will need to provide more information. At the very least. please provide the exact error message (you write "error like". Please copy/paste the full message. Also show us what #temp looks like. Column names, and data types.
– SchmitzIT
Nov 8 at 8:25
That was a typo its actually #Temp i am using sql server 2014
– Amateur.techie
Nov 8 at 8:29
1
Why aren't you just doingSelect * from tab1 where col1 in (select var1 from #Temp)
?
– Damien_The_Unbeliever
Nov 8 at 8:51
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a temp table where i am creating all the values of column to a comma separated list.. the issue i am facing is when i am inputting that csv variable i am getting error
For ex
Declare @testvar varchar (max)
Select @testvar = stuff (( select (','''+ cast(var1 as varchar(1024)+'''')
From # Temp for xmlpath('')),1,2,'') from #Temp
Select * from tab1 where col1 in (@testvar)
This is appending single quotes in query i am getting error like
Conversion failed while converting varchar value ''1','2'........ to int
Any suggestions for this
sql

I have a temp table where i am creating all the values of column to a comma separated list.. the issue i am facing is when i am inputting that csv variable i am getting error
For ex
Declare @testvar varchar (max)
Select @testvar = stuff (( select (','''+ cast(var1 as varchar(1024)+'''')
From # Temp for xmlpath('')),1,2,'') from #Temp
Select * from tab1 where col1 in (@testvar)
This is appending single quotes in query i am getting error like
Conversion failed while converting varchar value ''1','2'........ to int
Any suggestions for this
sql

sql

edited Nov 8 at 8:48


Rahul Neekhra
391423
391423
asked Nov 8 at 8:20
Amateur.techie
215
215
Which dbms are you using?
– jarlh
Nov 8 at 8:21
2
# Temp
would be a wrong syntax
– SchmitzIT
Nov 8 at 8:23
You will need to provide more information. At the very least. please provide the exact error message (you write "error like". Please copy/paste the full message. Also show us what #temp looks like. Column names, and data types.
– SchmitzIT
Nov 8 at 8:25
That was a typo its actually #Temp i am using sql server 2014
– Amateur.techie
Nov 8 at 8:29
1
Why aren't you just doingSelect * from tab1 where col1 in (select var1 from #Temp)
?
– Damien_The_Unbeliever
Nov 8 at 8:51
|
show 2 more comments
Which dbms are you using?
– jarlh
Nov 8 at 8:21
2
# Temp
would be a wrong syntax
– SchmitzIT
Nov 8 at 8:23
You will need to provide more information. At the very least. please provide the exact error message (you write "error like". Please copy/paste the full message. Also show us what #temp looks like. Column names, and data types.
– SchmitzIT
Nov 8 at 8:25
That was a typo its actually #Temp i am using sql server 2014
– Amateur.techie
Nov 8 at 8:29
1
Why aren't you just doingSelect * from tab1 where col1 in (select var1 from #Temp)
?
– Damien_The_Unbeliever
Nov 8 at 8:51
Which dbms are you using?
– jarlh
Nov 8 at 8:21
Which dbms are you using?
– jarlh
Nov 8 at 8:21
2
2
# Temp
would be a wrong syntax– SchmitzIT
Nov 8 at 8:23
# Temp
would be a wrong syntax– SchmitzIT
Nov 8 at 8:23
You will need to provide more information. At the very least. please provide the exact error message (you write "error like". Please copy/paste the full message. Also show us what #temp looks like. Column names, and data types.
– SchmitzIT
Nov 8 at 8:25
You will need to provide more information. At the very least. please provide the exact error message (you write "error like". Please copy/paste the full message. Also show us what #temp looks like. Column names, and data types.
– SchmitzIT
Nov 8 at 8:25
That was a typo its actually #Temp i am using sql server 2014
– Amateur.techie
Nov 8 at 8:29
That was a typo its actually #Temp i am using sql server 2014
– Amateur.techie
Nov 8 at 8:29
1
1
Why aren't you just doing
Select * from tab1 where col1 in (select var1 from #Temp)
?– Damien_The_Unbeliever
Nov 8 at 8:51
Why aren't you just doing
Select * from tab1 where col1 in (select var1 from #Temp)
?– Damien_The_Unbeliever
Nov 8 at 8:51
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You seem to like doing dynamic SQL. make sure your brackets are all corresponded and correctly.
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
Select * from tab1 where col1 in (@testvar)
sqlfiddle
Here is a sample
CREATE TABLE #Temp(
var1 INT
);
INSERT INTO #Temp VALUES (1);
INSERT INTO #Temp VALUES (2);
create table tab1(
col1 int
);
insert into tab1 values (1)
insert into tab1 values (3)
Declare @testvar varchar (max)
Declare @query varchar (max)
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
SET @query = 'Select * from tab1 where col1 in ('+@testvar+')'
execute(@QUERY)
Thanks it worked for me
– Amateur.techie
Nov 8 at 12:05
add a comment |
up vote
1
down vote
The IN
operator expects a list of values. You are supplying it with a single value that happens to contain a comma delimited text. There's a difference that seems very subtle as a human, but to SQL Server it's a huge difference.
A query like
SELECT * FROM Tab1 WHERE Col1 IN ('A', 'B', 'C')
will return all rows where col1 has the value A
, B
or C
, while a query like
SELECT * FROM Tab1 WHERE Col1 IN ('''A'', ''B'', ''C''')
will return only the records where col1 has the value 'A', 'B', 'C'
- so basically it's like writing
SELECT * FROM Tab1 WHERE Col1 = '''A'', ''B'', ''C'''
What you should do is using the temporary table directly:
SELECT *
FROM Tab1
WHERE EXISTS
(
SELECT 1
FROM #Temp as tmp
WHERE tmp.Var1 = Tab1.Col1
)
Thanks alot 😊😊😊
– Amateur.techie
Nov 8 at 12:06
Glad to help :-)
– Zohar Peled
Nov 8 at 12:14
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You seem to like doing dynamic SQL. make sure your brackets are all corresponded and correctly.
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
Select * from tab1 where col1 in (@testvar)
sqlfiddle
Here is a sample
CREATE TABLE #Temp(
var1 INT
);
INSERT INTO #Temp VALUES (1);
INSERT INTO #Temp VALUES (2);
create table tab1(
col1 int
);
insert into tab1 values (1)
insert into tab1 values (3)
Declare @testvar varchar (max)
Declare @query varchar (max)
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
SET @query = 'Select * from tab1 where col1 in ('+@testvar+')'
execute(@QUERY)
Thanks it worked for me
– Amateur.techie
Nov 8 at 12:05
add a comment |
up vote
1
down vote
accepted
You seem to like doing dynamic SQL. make sure your brackets are all corresponded and correctly.
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
Select * from tab1 where col1 in (@testvar)
sqlfiddle
Here is a sample
CREATE TABLE #Temp(
var1 INT
);
INSERT INTO #Temp VALUES (1);
INSERT INTO #Temp VALUES (2);
create table tab1(
col1 int
);
insert into tab1 values (1)
insert into tab1 values (3)
Declare @testvar varchar (max)
Declare @query varchar (max)
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
SET @query = 'Select * from tab1 where col1 in ('+@testvar+')'
execute(@QUERY)
Thanks it worked for me
– Amateur.techie
Nov 8 at 12:05
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You seem to like doing dynamic SQL. make sure your brackets are all corresponded and correctly.
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
Select * from tab1 where col1 in (@testvar)
sqlfiddle
Here is a sample
CREATE TABLE #Temp(
var1 INT
);
INSERT INTO #Temp VALUES (1);
INSERT INTO #Temp VALUES (2);
create table tab1(
col1 int
);
insert into tab1 values (1)
insert into tab1 values (3)
Declare @testvar varchar (max)
Declare @query varchar (max)
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
SET @query = 'Select * from tab1 where col1 in ('+@testvar+')'
execute(@QUERY)
You seem to like doing dynamic SQL. make sure your brackets are all corresponded and correctly.
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
Select * from tab1 where col1 in (@testvar)
sqlfiddle
Here is a sample
CREATE TABLE #Temp(
var1 INT
);
INSERT INTO #Temp VALUES (1);
INSERT INTO #Temp VALUES (2);
create table tab1(
col1 int
);
insert into tab1 values (1)
insert into tab1 values (3)
Declare @testvar varchar (max)
Declare @query varchar (max)
set @testvar= stuff(
(select ',''' + cast(var1 as varchar(1024))+ ''''
From #Temp for xml path(''))
,1,1,'')
SET @query = 'Select * from tab1 where col1 in ('+@testvar+')'
execute(@QUERY)
edited Nov 8 at 8:46
answered Nov 8 at 8:39


D-Shih
23.4k61331
23.4k61331
Thanks it worked for me
– Amateur.techie
Nov 8 at 12:05
add a comment |
Thanks it worked for me
– Amateur.techie
Nov 8 at 12:05
Thanks it worked for me
– Amateur.techie
Nov 8 at 12:05
Thanks it worked for me
– Amateur.techie
Nov 8 at 12:05
add a comment |
up vote
1
down vote
The IN
operator expects a list of values. You are supplying it with a single value that happens to contain a comma delimited text. There's a difference that seems very subtle as a human, but to SQL Server it's a huge difference.
A query like
SELECT * FROM Tab1 WHERE Col1 IN ('A', 'B', 'C')
will return all rows where col1 has the value A
, B
or C
, while a query like
SELECT * FROM Tab1 WHERE Col1 IN ('''A'', ''B'', ''C''')
will return only the records where col1 has the value 'A', 'B', 'C'
- so basically it's like writing
SELECT * FROM Tab1 WHERE Col1 = '''A'', ''B'', ''C'''
What you should do is using the temporary table directly:
SELECT *
FROM Tab1
WHERE EXISTS
(
SELECT 1
FROM #Temp as tmp
WHERE tmp.Var1 = Tab1.Col1
)
Thanks alot 😊😊😊
– Amateur.techie
Nov 8 at 12:06
Glad to help :-)
– Zohar Peled
Nov 8 at 12:14
add a comment |
up vote
1
down vote
The IN
operator expects a list of values. You are supplying it with a single value that happens to contain a comma delimited text. There's a difference that seems very subtle as a human, but to SQL Server it's a huge difference.
A query like
SELECT * FROM Tab1 WHERE Col1 IN ('A', 'B', 'C')
will return all rows where col1 has the value A
, B
or C
, while a query like
SELECT * FROM Tab1 WHERE Col1 IN ('''A'', ''B'', ''C''')
will return only the records where col1 has the value 'A', 'B', 'C'
- so basically it's like writing
SELECT * FROM Tab1 WHERE Col1 = '''A'', ''B'', ''C'''
What you should do is using the temporary table directly:
SELECT *
FROM Tab1
WHERE EXISTS
(
SELECT 1
FROM #Temp as tmp
WHERE tmp.Var1 = Tab1.Col1
)
Thanks alot 😊😊😊
– Amateur.techie
Nov 8 at 12:06
Glad to help :-)
– Zohar Peled
Nov 8 at 12:14
add a comment |
up vote
1
down vote
up vote
1
down vote
The IN
operator expects a list of values. You are supplying it with a single value that happens to contain a comma delimited text. There's a difference that seems very subtle as a human, but to SQL Server it's a huge difference.
A query like
SELECT * FROM Tab1 WHERE Col1 IN ('A', 'B', 'C')
will return all rows where col1 has the value A
, B
or C
, while a query like
SELECT * FROM Tab1 WHERE Col1 IN ('''A'', ''B'', ''C''')
will return only the records where col1 has the value 'A', 'B', 'C'
- so basically it's like writing
SELECT * FROM Tab1 WHERE Col1 = '''A'', ''B'', ''C'''
What you should do is using the temporary table directly:
SELECT *
FROM Tab1
WHERE EXISTS
(
SELECT 1
FROM #Temp as tmp
WHERE tmp.Var1 = Tab1.Col1
)
The IN
operator expects a list of values. You are supplying it with a single value that happens to contain a comma delimited text. There's a difference that seems very subtle as a human, but to SQL Server it's a huge difference.
A query like
SELECT * FROM Tab1 WHERE Col1 IN ('A', 'B', 'C')
will return all rows where col1 has the value A
, B
or C
, while a query like
SELECT * FROM Tab1 WHERE Col1 IN ('''A'', ''B'', ''C''')
will return only the records where col1 has the value 'A', 'B', 'C'
- so basically it's like writing
SELECT * FROM Tab1 WHERE Col1 = '''A'', ''B'', ''C'''
What you should do is using the temporary table directly:
SELECT *
FROM Tab1
WHERE EXISTS
(
SELECT 1
FROM #Temp as tmp
WHERE tmp.Var1 = Tab1.Col1
)
edited Nov 8 at 9:12
answered Nov 8 at 9:01


Zohar Peled
50.4k73070
50.4k73070
Thanks alot 😊😊😊
– Amateur.techie
Nov 8 at 12:06
Glad to help :-)
– Zohar Peled
Nov 8 at 12:14
add a comment |
Thanks alot 😊😊😊
– Amateur.techie
Nov 8 at 12:06
Glad to help :-)
– Zohar Peled
Nov 8 at 12:14
Thanks alot 😊😊😊
– Amateur.techie
Nov 8 at 12:06
Thanks alot 😊😊😊
– Amateur.techie
Nov 8 at 12:06
Glad to help :-)
– Zohar Peled
Nov 8 at 12:14
Glad to help :-)
– Zohar Peled
Nov 8 at 12:14
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203784%2fsql-conversion-issue%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
xAXD FvKzRnOh,N7j ZfBl 6dI7,lnBEaUxaBhd9NyQuBlRCNy8,mr6d7eOJGHG l,WlHU,FUU
Which dbms are you using?
– jarlh
Nov 8 at 8:21
2
# Temp
would be a wrong syntax– SchmitzIT
Nov 8 at 8:23
You will need to provide more information. At the very least. please provide the exact error message (you write "error like". Please copy/paste the full message. Also show us what #temp looks like. Column names, and data types.
– SchmitzIT
Nov 8 at 8:25
That was a typo its actually #Temp i am using sql server 2014
– Amateur.techie
Nov 8 at 8:29
1
Why aren't you just doing
Select * from tab1 where col1 in (select var1 from #Temp)
?– Damien_The_Unbeliever
Nov 8 at 8:51