我需要在sql server中编写一个查询,让我在我的表中注册月份,并以某种方式将这些月份转换为4个季节(秋季,秋季,春季,夏季)。有谁知道怎么做?
答案 0 :(得分:4)
一种方法是case
。你的问题并没有澄清逻辑,但这是一个例子:
(case when month(date) in (12, 1, 2) then 'winter'
when month(date) in (3, 4, 5) then 'spring'
when month(date) in (6, 7, 8) then 'summer'
when month(date) in (9, 10, 11) then 'autumn'
end) as season
答案 1 :(得分:1)
(CHOOSE(month(TheDate),'Winter', 'Winter', 'Spring', 'Spring', 'Spring',
'Summer', 'Summer', 'Summer', 'Autumn', 'Autumn', 'Autumn', 'Winter'))
答案 2 :(得分:0)
将linoff的答案改为:
case dateatepart(quarter,idate)
when 1 then 'winter'
when 2 then 'spring'
when 3 then 'summer'
when 4 then 'autumn'
end as season