表中数据中日期为月未的日期,现在想根据给定的日期取出相应的数据即:
1.若给定的日期为月未的日期则取中相应日期的数据.
2.若给定的日期不是月未的日期则取出上月未相应的数据.
例如:
表中
a 2009-01-31
b 2009-02-28
c 2009-03-31苦给定日期为2009-02-28
则得到
b 2009-02-28苦给定日期为2009-02-20
则得到
a 2009-01-31
1.若给定的日期为月未的日期则取中相应日期的数据.
2.若给定的日期不是月未的日期则取出上月未相应的数据.
例如:
表中
a 2009-01-31
b 2009-02-28
c 2009-03-31苦给定日期为2009-02-28
则得到
b 2009-02-28苦给定日期为2009-02-20
则得到
a 2009-01-31
case when datepart(d,dateadd(dd,1,日期))=1 then 日期
else dateadd(dd,-datepart(d,日期),日期)
set @input='2009-02-20'select * from T
where 日期= case when datediff(month,@input,dateadd(day,1,@input))=1
then @input
else dateadd(day,-1,convert(char(07),@input,120)+'-01')
end
set @date ='2009/02/28'
if (@date=dateadd(dd,-1,dateadd(mm,1,convert(char(7),@date,111)+'/01')))
select @date
else
select dateadd(dd,-1,convert(char(7),@date,111)+'/01')
/*----------
2009/02/28(1 行受影响)*/
谢谢你
我是一个查询语句这语句放在那?
select *
from tab
where .. and ...
select (case when datepart(d,dateadd(dd,1,日期))=1 then 日期
else dateadd(dd,-datepart(d,日期),日期) end ) as 日期
from tab
如果是月末日期,则日期加1就是月初日期,就是1 号,否则就不是月末日期。
select *,case when coldate=dateadd(dd,-1,dateadd(mm,1,convert(char(7),coldate,111)+'/01'))
then coldate
else dateadd(dd,-1,convert(char(7),coldate,111)+'/01') end
from tab
where .. and ...
select (case when datepart(d,dateadd(dd,1,日期))=1 then 日期
else dateadd(dd,-datepart(d,日期),日期) end ) as 日期
from tab
格式没有对好。写代码要有一个良好的风格。