Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get output to not write more SQL CASES using select query

I have below xyz table data

id    ADivision     BDivision   CDivision     DDivision  EDivision     FDivision
 1       0              1           0           0            1           0
 2       1              1           0           0            1           1

I want output like below

 id    Divisions
 1     B-E
 2     A-B-E-F

I tried by using switch case but need to write more cases

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

any other ways to get output

>Solution :

The CONCAT_WS() function is your friend here:

SELECT
    id,
    CONCAT_WS('-', CASE WHEN ADivision = 1 THEN 'A' END,
                   CASE WHEN BDivision = 1 THEN 'B' END,
                   CASE WHEN CDivision = 1 THEN 'C' END,
                   CASE WHEN DDivision = 1 THEN 'D' END,
                   CASE WHEN EDivision = 1 THEN 'E' END,
                   CASE WHEN FDivision = 1 THEN 'F' END) AS Divisions
FROM yourTable
ORDER BY id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading