SQL 计算每个月的第一天,最后一天,本月的天数。

sql server:

--frist day

select dateadd(dd,-datepart(dd,getdate())+1,getdate())

--last day
select dateadd(dd,-datepart(dd,getdate()) ,dateadd(mm,1,getdate()))

--next month first day

select dateadd(dd,-datepart(dd,getdate())+1,dateadd(mm,1,getdate()))

--本月天数

select datediff(dd,dateadd(dd,-datepart(dd,getdate())+1,getdate()), dateadd(dd,-datepart(dd,getdate())+1,dateadd(mm,1,getdate())))

or

select datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(getdate()) as varchar)+'-'+cast(month(getdate()) as varchar)+'-01' as datetime))))

 

oracle

--本月天数

select to_number(to_char(last_day(sysdate),'DD')) from dual

posted on 2013-03-27 16:01  rxinfo  阅读(390)  评论(0编辑  收藏  举报