Solved

# sql server calculate age range with case

Posted on 2012-08-14

Trying to calculate an age range of the records in my table. Someone please check my syntax as something is not right.

select AGE = CASE

when BETWEEN '18' AND '25' then '18-25'

When Between '26' AND '30' then '26-30'

When Between '31' AND '35' then '31-35'

When between '36' AND '40' then '36-40'

when between '41' and '45' then '41-45'

when between '46' and '50' then '46-50'

when between '51' and '55' then '51-44'

when between '56' and '60' then '56-60'

when between '61' and '65' then '61-65'

when between '66' and '70' then '66-70'

when between '71' and '75' then '71-75'

when between '76' and '80' then '76-80'

--when <= 60 then 'Over 80'

End,

COUNT(*) as TotalVoters

From vwPersonSearch

Group by Case vf.age

when BETWEEN '18' AND '25' then '18-25'

When Between '26' AND '30' then '26-30'

When Between '31' AND '35' then '31-35'

When between '36' AND '40' then '36-40'

when between '41' and '45' then '41-45'

when between '46' and '50' then '46-50'

when between '51' and '55' then '51-44'

when between '56' and '60' then '56-60'

when between '61' and '65' then '61-65'

when between '66' and '70' then '66-70'

when between '71' and '75' then '71-75'

when between '76' and '80' then '76-80'

when <= '60' then 'Over 80'

end