MariaHalt
asked on
sql function needed for csv list of fields
Hi All, This is legacy stuff that I have to make work. I need a function to return a comma separated list of field names for each record. Let's pretend this is the scenario.
--DROP TABLE tempdb.dbo.mh_account_chan ge_request s
CREATE TABLE tempdb.dbo.mh_account_chan ge_request s (
acr_id INT NOT NULL,
acr_fname VARCHAR(255) NULL,
acr_fname_change BIT NOT NULL,
acr_mname VARCHAR(255) NULL,
acr_mname_change BIT NOT NULL,
acr_lname VARCHAR(255) NULL,
acr_lname_change BIT NOT NULL)
INSERT INTO tempdb.dbo.mh_account_chan ge_request s VALUES (1, 'Greg', 1, NULL, 0, 'Brady', 1)
INSERT INTO tempdb.dbo.mh_account_chan ge_request s VALUES (2, NULL, 0, 'Pete', 1, 'Brady', 1)
INSERT INTO tempdb.dbo.mh_account_chan ge_request s VALUES (3, 'Bobby', 1, 'Brady', 1, NULL, 0)
SELECT *, <plus a comma separated list OF the change field NAME> AS changerequests FROM tempdb.dbo.mh_account_chan ge_request s
For results that look like this:
--DROP TABLE tempdb.dbo.mh_account_chan
CREATE TABLE tempdb.dbo.mh_account_chan
acr_id INT NOT NULL,
acr_fname VARCHAR(255) NULL,
acr_fname_change BIT NOT NULL,
acr_mname VARCHAR(255) NULL,
acr_mname_change BIT NOT NULL,
acr_lname VARCHAR(255) NULL,
acr_lname_change BIT NOT NULL)
INSERT INTO tempdb.dbo.mh_account_chan
INSERT INTO tempdb.dbo.mh_account_chan
INSERT INTO tempdb.dbo.mh_account_chan
SELECT *, <plus a comma separated list OF the change field NAME> AS changerequests FROM tempdb.dbo.mh_account_chan
For results that look like this:
Does this help: http://social.msdn.microsoft.com/forums/en-US/sqlgetstarted/thread/812b8eec-5b77-42a2-bd23-965558ece5b9/
ASKER
I only want the fields where the bit = 1.
ASKER
Also, I don't want to export them to a file, I just need the results in an additional field.
I experienced an error whilst testing this, it wouldn't let me create a row of column names and then union that with a full select from the table as some of the fields where set to 'int' and the column names weren't integers.
My code was:
And I got the error:
However if you were to cast or convert each integer field to nchar in your main select statement it may work!
My code was:
select 'Code','Description','Address1','Address2','Address3','Address4','Postcode','Tel'
union
select * from branches
And I got the error:
Conversion failed when converting the varchar value 'Code' to data type int.
However if you were to cast or convert each integer field to nchar in your main select statement it may work!
First off, that you so much for providing some sample SQL code. It is so very helpful!!!
I'm thinking you want to use STUFF along with XML PATH. (This is basically the same as MySQL's GROUP_CONCAT function), but usually that means grouping by something (a key?)
In your example code, what is the comma seperated list that you're looking for/results that you want?
You most likely want something using this code - I'm just trying to figure out what it is you exactly want.
I'm thinking you want to use STUFF along with XML PATH. (This is basically the same as MySQL's GROUP_CONCAT function), but usually that means grouping by something (a key?)
In your example code, what is the comma seperated list that you're looking for/results that you want?
You most likely want something using this code - I'm just trying to figure out what it is you exactly want.
SELECT STUFF(
(SELECT ',' + Column_Name
FROM Table_Name
FOR XML PATH (','))
, 1, 1, '')
BurundiLapp - I think you posted this to wrong thread question.
I think I may have, sorry OP.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Beyond Excellent! Thank you!!! Exactly what I wanted.