SQL Conversion Issue











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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













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










share|improve this question















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-server tsql sql-server-2014






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 doing Select * 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






  • 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
















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












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)





share|improve this answer























  • Thanks it worked for me
    – Amateur.techie
    Nov 8 at 12:05


















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
)





share|improve this answer























  • Thanks alot 😊😊😊
    – Amateur.techie
    Nov 8 at 12:06










  • Glad to help :-)
    – Zohar Peled
    Nov 8 at 12:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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)





share|improve this answer























  • Thanks it worked for me
    – Amateur.techie
    Nov 8 at 12:05















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)





share|improve this answer























  • Thanks it worked for me
    – Amateur.techie
    Nov 8 at 12:05













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)





share|improve this answer














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)






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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












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
)





share|improve this answer























  • Thanks alot 😊😊😊
    – Amateur.techie
    Nov 8 at 12:06










  • Glad to help :-)
    – Zohar Peled
    Nov 8 at 12:14















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
)





share|improve this answer























  • Thanks alot 😊😊😊
    – Amateur.techie
    Nov 8 at 12:06










  • Glad to help :-)
    – Zohar Peled
    Nov 8 at 12:14













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
)





share|improve this answer














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
)






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff