Solved

sql function needed for csv list of fields

Posted on 2013-06-13
9
243 Views
Last Modified: 2013-06-21
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_change_requests

CREATE TABLE tempdb.dbo.mh_account_change_requests (
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_change_requests VALUES (1, 'Greg', 1, NULL, 0, 'Brady', 1)
INSERT INTO tempdb.dbo.mh_account_change_requests VALUES (2, NULL, 0, 'Pete', 1, 'Brady', 1)
INSERT INTO tempdb.dbo.mh_account_change_requests 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_change_requests

For results that look like this:
desired results
0
Comment
Question by:MariaHalt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39245039
0
 

Author Comment

by:MariaHalt
ID: 39245060
I only want the fields where the bit = 1.
0
 

Author Comment

by:MariaHalt
ID: 39245064
Also, I don't want to export them to a file, I just need the results in an additional field.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39245124
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:
select 'Code','Description','Address1','Address2','Address3','Address4','Postcode','Tel'

union

select * from branches

Open in new window


And I got the error:

Conversion failed when converting the varchar value 'Code' to data type int.

Open in new window


However if you were to cast or convert each integer field to nchar in your main select statement it may work!
0
 
LVL 23

Expert Comment

by:nemws1
ID: 39245155
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.

SELECT STUFF(
             (SELECT ',' + Column_Name 
              FROM Table_Name
              FOR XML PATH (','))
             , 1, 1, '')

Open in new window

0
 
LVL 23

Expert Comment

by:nemws1
ID: 39245160
BurundiLapp - I think you posted this to wrong thread question.
0
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39245165
I think I may have, sorry OP.
0
 
LVL 4

Accepted Solution

by:
BAKADY earned 500 total points
ID: 39245572
SELECT *,
  STUFF((
    SELECT ', ' + COLUMN_NAME 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_NAME = 'mh_account_change_requests'
       AND (
          (COLUMN_NAME = 'acr_fname' AND Not acr_fname Is NULL)
         OR
          (COLUMN_NAME = 'acr_mname' AND Not acr_mname Is NULL)
         OR
          (COLUMN_NAME = 'acr_lname' AND Not acr_lname Is NULL)
         )
    ORDER BY ordinal_position
    FOR XML PATH('')
  ),1,2,'')
FROM mh_account_change_requests

Open in new window


to try the code above look by SQL-Fiddle

http://sqlfiddle.com/#!3/a88e3/3
0
 

Author Closing Comment

by:MariaHalt
ID: 39266285
Beyond Excellent!  Thank you!!! Exactly what I wanted.
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question