0

I was wondering if it is possible to change a CASE when then else to a union all, or something like this.

I hope that it is possible ...

example

CASE WHEN(C.Salutation = 'Dhr.' OR C.salutation = 'dhr.')
         THEN 'Meneer'+' '+ISNULL(C.middlename+' ','')+C.Lastname
    WHEN(C.Salutation = 'Mw.' OR C.salutation = 'mw.')
         THEN 'Mevrouw'+' '+ISNULL(C.middlename+' ','')+C.Lastname
    ELSE 'Meneer/Mevrouw '+ISNULL(C.middlename+' ','')+ISNULL(C.Lastname,'') END AS 'K5'

i hope you guys can help me out..

4
  • Whould it make sence? CASE is just what you need here
    – Alex
    Apr 18, 2013 at 9:01
  • yeah but the man i'm doing this for want it in a diffrent way.. but i,ve google it and i can find something...
    – Jeroen
    Apr 18, 2013 at 9:04
  • 1
    If this is being used as part of the SELECT (not part of the WHERE) then CASE is the best way to do this. It would be worth pushing back on the man you're doing this for and suggesting it stay this way. However Scorpi0 has the correct answer shown. Apr 18, 2013 at 9:22
  • thanks i should tell him this.
    – Jeroen
    Apr 18, 2013 at 9:32

1 Answer 1

0

Well, yes you can, but it is ugly:

SELECT 'Meneer'+' '+ISNULL(C1.middlename+' ','')+C1.Lastname AS 'K5'
FROM table C1
WHERE C1.Salutation = 'Dhr.' OR C1.salutation = 'dhr.'
UNION
SELECT 'Mevrouw'+' '+ISNULL(C2.middlename+' ','')+C2.Lastname
FROM table C2
WHERE C2.Salutation = 'Mw.' OR C2.salutation = 'mw.'
UNION
SELECT 'Meneer/Mevrouw '+ISNULL(C.middlename+' ','')+ISNULL(C.Lastname,'')
FROM table C3
WHERE C3.Salutation != 'Mw.' AND C3.salutation != 'mw.'
AND C3.Salutation != 'Dhr.' AND C3.salutation != 'dhr.'
2
  • @Jeroen Note though that just 'UNION' will return only unique rows. If you want to see duplicates as well you should use 'UNION ALL'
    – Artemix
    May 15, 2013 at 14:39
  • i know now i am using UNION ALL
    – Jeroen
    May 15, 2013 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.