Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

TSQL function to retrieve multiple values

Posted on 2016-08-24
6
Medium Priority
?
71 Views
Last Modified: 2016-08-24
Hello,

Uh oh!  SQL guy is on vacation again!  

I'm trying to feed a "Race" field on an RDLC report.  My database has a person table with bit fields to indicate the race(s) a person belongs to.

The layout is something like this:
person_ID | name | DOB | IsAsian | IsBlack | IsNativeHawaiian | IsAlaskaNative | IsWhite

A person can belong to more than one race group.  I'm trying to write a function that would output a string of each race the person belongs to.  For example, if IsAlaskaNative='1' AND IsWhite='1' the output would be "Alaskan Native, White".

Any help you can provide will be greatly appreciated.  
Thanks!
0
Comment
Question by:ttist25
6 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 400 total points
ID: 41768849
Eyeballeth thy article T-SQL:  Normalized data to a single comma delineated string and back, which unfortunately is the easiest way to pull this off.
0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 360 total points
ID: 41768850
try simply using IIF

SELECT person_id, IIF(IsAsian=1, 'Asian,', '') + IIF(IsBlack=1, 'Black,', '') + IIF(IsNativeHawaiian=1, 'Native Hawaiian,', '') + IIF(IsAlaskaNative=1, 'Alaskan Native,', '') + IIF(IsWhite=1, 'White', '') AS Race
FROM TABLE1

Open in new window

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 41768853
SELECT STUFF(
    CASE WHEN raceBits & 1 > 0 THEN ', Asian' ELSE '' END +
    CASE WHEN raceBits & 2 > 0 THEN ', Black' ELSE '' END +    
    CASE WHEN raceBits & 4 > 0 THEN ', Alaskan Native' ELSE '' END +
    CASE WHEN raceBits & 8 > 0 THEN ', White' ELSE '' END,
    1, 2, '') AS Race
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 17

Assisted Solution

by:dbaSQL
dbaSQL earned 240 total points
ID: 41768878
Not a function, but this will work:

--The layout is something like this:
--person_ID | name | DOB | IsAsian | IsBlack | IsNativeHawaiian | IsAlaskaNative | IsWhite

create table #layout (personID int, name varchar(25), DOB char(8), IsAsian BIT, IsBlack BIT, IsNativeHawaiian BIT, IsAlaskaNative BIT, IsWhite BIT)
INSERT #layout (personID,name,DOB,IsAsian,IsBlack,IsNativeHawaiian,IsAlaskaNative,IsWhite)
VALUES (1,'John Doe','07-02-71',0,0,1,0,1),
(2,'Jane Doe','07-03-71',0,1,0,0,0);

--A person can belong to more than one race group.  I'm trying to write a function that would output a string
--of each race the person belongs to.  For example, if IsAlaskaNative='1' AND IsWhite='1' the output would be
--"Alaskan Native, White".

SELECT
      PersonID,
      Name,
      DOB,
      CASE WHEN IsAsian = 1 THEN 'Asian'
             WHEN IsBlack = 1 THEN 'Black'
             WHEN IsNativeHawaiian = 1 AND IsWhite = 1 THEN  'Native Hawaiian, White'
             WHEN IsNativeHawaiian = 1 AND IsWhite = 0 THEN 'Native Hawaiian'
             WHEN IsAlaskaNative = 1 AND IsWhite = 1 THEN 'Alaskan Native, White'
             WHEN IsAlaskaNative = 1 AND IsWhite = 0 THEN 'Alaskan Native'
             WHEN IsWhite = 1 AND IsNativeHawaiian = 0 AND IsAlaskaNative = 0 THEN 'White' END [Race]
FROM
      #layout


PersonID      Name      DOB      Race
1      John Doe      07-02-71         Native Hawaiian, White
2      Jane Doe              07-03-71   Black
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 41768941
I need to adjust my code.  Sorry, I originally thought all the bits were in one int column, not separate bit columns.  The core of the code is the same:

SELECT STUFF(
    CASE WHEN isAsian = 1 THEN ', Asian' ELSE '' END +
    CASE WHEN isBlack = 1 THEN ', Black' ELSE '' END +    
    CASE WHEN isAlaskanNative = 1 THEN 'Alaskan Native' ELSE '' END +
    CASE WHEN isWhite = 1 THEN ', White' ELSE '' END,
    1, 2, '') AS Race
0
 
LVL 1

Author Closing Comment

by:ttist25
ID: 41769177
This is the craftsmanship of coding.  :)  Different ways to get the same result.

Thanks so much for all of your help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

886 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