Solved

TSQL function to retrieve multiple values

Posted on 2016-08-24
6
61 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
[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
6 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 100 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 90 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 69

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 17

Assisted Solution

by:dbaSQL
dbaSQL earned 60 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 69

Accepted Solution

by:
Scott Pletcher earned 250 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

615 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