由于会计计帐所致,要求1月1日-1月25日 输出1
1月26日-2月25日 输出2
2月26日-3月25日 输出3
3月26日-4月25日 输出4
4月26日-5月25日 输出5
5月26日-6月25日 输出6
6月26日-7月25日 输出7
7月26日-8月25日 输出8
8月26日-9月25日 输出9
9月26日-10月25日 输出10
10月26日-11月25日 输出11
11月26日-12月31日 输出12这样的SQL语句该怎么写才比较简洁,谢谢大家了!
1月26日-2月25日 输出2
2月26日-3月25日 输出3
3月26日-4月25日 输出4
4月26日-5月25日 输出5
5月26日-6月25日 输出6
6月26日-7月25日 输出7
7月26日-8月25日 输出8
8月26日-9月25日 输出9
9月26日-10月25日 输出10
10月26日-11月25日 输出11
11月26日-12月31日 输出12这样的SQL语句该怎么写才比较简洁,谢谢大家了!
then dateadd(day,6,ddate)
else ddate end)
from rdrecord
select 输出=case dt
when between cast(convert(char(8),dt,120)+'01-01' as datetime) and cast(convert(char(8),dt,120)+'01-25' as datetime) then 1
when between cast(convert(char(8),dt,120)+'01-26' as datetime) and cast(convert(char(8),dt,120)+'02-25' as datetime) then 2
when between cast(convert(char(8),dt,120)+'02-26' as datetime) and cast(convert(char(8),dt,120)+'03-25' as datetime) then 3
when between cast(convert(char(8),dt,120)+'03-26' as datetime) and cast(convert(char(8),dt,120)+'04-25' as datetime) then 4
when between cast(convert(char(8),dt,120)+'04-26' as datetime) and cast(convert(char(8),dt,120)+'05-25' as datetime) then 5
when between cast(convert(char(8),dt,120)+'05-26' as datetime) and cast(convert(char(8),dt,120)+'06-25' as datetime) then 6
when between cast(convert(char(8),dt,120)+'06-26' as datetime) and cast(convert(char(8),dt,120)+'07-25' as datetime) then 7
when between cast(convert(char(8),dt,120)+'07-26' as datetime) and cast(convert(char(8),dt,120)+'08-25' as datetime) then 8
when between cast(convert(char(8),dt,120)+'08-26' as datetime) and cast(convert(char(8),dt,120)+'09-25' as datetime) then 9
when between cast(convert(char(8),dt,120)+'09-26' as datetime) and cast(convert(char(8),dt,120)+'10-25' as datetime) then 10
when between cast(convert(char(8),dt,120)+'10-26' as datetime) and cast(convert(char(8),dt,120)+'11-25' as datetime) then 11
when between cast(convert(char(8),dt,120)+'11-26' as datetime) and cast(convert(char(8),dt,120)+'12-31' as datetime) then 12
from tb
create table tb (dd datetime)insert tb
select '2009-01-12' union all
select '2009-02-27' union all
select '2009-03-15' union all
select '2009-12-30' select dd,(case when day(dd)<25 then month(dd)
when day(dd)>25 and month(dd)<12 then month(dd)+1
else month(dd)
end) mm from tb-------------------------------
dd mm
2009-01-12 00:00:00.000 1
2009-02-27 00:00:00.000 3
2009-03-15 00:00:00.000 3
2009-12-30 00:00:00.000 12