We help IT Professionals succeed at work.

adding quotes to a comma separated string

Last Modified: 2020-04-10
Hi experts,

I'm using sql server 2019.

Query 1

In this query a comma delimited string is stored in a string variable called strvar1 and then that string variable is inserted into a temporary table.

-- declare a string variable
DECLARE @strvar1 char(100);
SET @strvar1 = 'LastName,FirstName,PlayerID';
-- create temp table called #tbl
CREATE TABLE #tbl ( line VARCHAR(1000) );
-- insert a comma delimited string into the temp table
INSERT #tbl VALUES ( @strvar1 );
-- display value of temp table

Open in new window

When I run Query 1  I get this:

current result set of Query 1

The string variable contains a value that looks like this:  LastName,FirstName,PlayerID

The reason i set the string variable value to this comma separated format is because that is how it is stored in the variable from a process.

How do I revise my query so each item in the comma separated string gets quotes added to it?

So my output of my  query should look like this:

desired output of query
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)


very cool solution. thanks.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.