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

Concatenation by using CASE statement

I want to CONCAT between this on my SELECT statement in SQL Server.

All the columns are booleans.

The output will be like this if all of columns that are true

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

GGPNES

I tried to use this and it doesn’t work

DECLARE @concat VARCHAR(40) ='';

SELECT
    Smoke, Invoice,
    Party, Summary,
    MySelf, Export,
    CASE
        WHEN MyTable.Smoke = 1 OR MyTable.Invoice = 1 
            THEN @concat + 'GG' 
        WHEN MyTable.Party = 1 
            THEN @concat + 'P'
        WHEN MyTable.Summary = 1 
            THEN @concat + 'N'
        WHEN MyTable.MySelf = 1 
            THEN @concat + 'E'
        WHEN MyTable.Export = 1 
            THEN @concat + 'S'
    END
FROM
    MyTable

>Solution :

I think that you want a concatenation of CASE expressions:

CASE WHEN Smoke = 1 OR Invoice = 1 THEN 'GG' ELSE '' END +
CASE WHEN Party = 1 THEN 'P' ELSE '' END +
CASE WHEN Summary = 1 THEN 'N' ELSE '' END +
CASE WHEN MySelf = 1 THEN 'E' ELSE '' END +
CASE WHEN Export = 1 THEN 'S' ELSE '' END
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