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 group update column by age range

I want to update the class column of 33-year-olds as 2, 34-35-year-olds class column as 3, 36-year-olds class column as 4. I use microsoft sql server.Is this possible?My table is like this:

ID DATE_OF_BIRTH CLASS
1  10.10.1986    1
2  11.02.1987    2
2  18.04.1988    3
.....................
.....................
.....................

>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

UPDATE YourTableName
SET CLASS = CASE WHEN DATEDIFF(year, DATE_OF_BIRTH, GETDATE()) = 33 THEN 2
                 WHEN DATEDIFF(year, DATE_OF_BIRTH , GETDATE())  BETWEEN 34 AND 35 THEN 3
                 WHEN DATEDIFF(year, DATE_OF_BIRTH , GETDATE()) = 36 THEN 4
                 ELSE CLASS
            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