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

SQL WHEN statements

I want to return the values in quotes if the DOB is within the dates shown, but the data comes back just showing the ELSE value only. Any ideas?

CASE
WHEN [Date of Birth] between 01/01/1945 and 31/12/1964 then 'Baby Boomer'
WHEN [Date of Birth] between 01/01/1965 and 31/12/1981 then 'Gen X'
WHEN [Date of Birth] between 01/01/1982 and 31/12/1994 then 'Millenial / Gen Y'
WHEN [Date of Birth] between 01/01/1995 and 31/12/2010 then 'Gen Z'
WHEN [Date of Birth] =>01/01/2011 then 'Gen Alpha'
ELSE 'Other TBC'
END

>Solution :

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

The dates should be in quotes, right? If this is TSQL:

Also, you should change ’31/12/1964′ to ’12/31/1964′ it should be MONTH then DAY then YEAR.

This works:

CASE
WHEN [Date of Birth] between '01/01/1945' and '12/31/1964' then 'Baby Boomer'
WHEN [Date of Birth] between '01/01/1965' and '12/31/1981' then 'Gen X'
WHEN [Date of Birth] between '01/01/1982' and '12/31/1994' then 'Millenial / Gen Y'
WHEN [Date of Birth] between '01/01/1995' and '12/31/2010' then 'Gen Z'
WHEN [Date of Birth] >= '01/01/2011' then 'Gen Alpha' -- changed => to >=
ELSE 'Other TBC'
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