日期:2014-05-18 浏览次数:20730 次
create table #t(ymd datetime) declare @year int declare @i int set @year = 2011 set @i = 1 while @i <= 12 begin insert into #t SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,convert(datetime,ltrim(@year)+right(100+@i,2)+'01'))+1, 0)) as ymd set @i = @i + 1 end select * from #t drop table #t /************** ymd ----------------------- 2011-01-31 23:59:59.997 2011-02-28 23:59:59.997 2011-03-31 23:59:59.997 2011-04-30 23:59:59.997 2011-05-31 23:59:59.997 2011-06-30 23:59:59.997 2011-07-31 23:59:59.997 2011-08-31 23:59:59.997 2011-09-30 23:59:59.997 2011-10-31 23:59:59.997 2011-11-30 23:59:59.997 2011-12-31 23:59:59.997 (12 行受影响)
------解决方案--------------------
每个月也是一样的啊
用master..spt_values表来构造时间。
------解决方案--------------------
DECLARE @dt DATETIME SET @dt=GETDATE() SET @dt=RTRIM(YEAR(@dt))+'0101' SELECT DATEADD(m,number,@dt) AS StartDate,DATEADD(m,number+1,@dt)-1 AS EndDate FROM master.dbo.spt_values WHERE type='P' AND DATEADD(m,number,@dt)<DATEadd(yy,1,@dt) /* StartDate EndDate 2011-01-01 00:00:00.000 2011-01-31 00:00:00.000 2011-02-01 00:00:00.000 2011-02-28 00:00:00.000 2011-03-01 00:00:00.000 2011-03-31 00:00:00.000 2011-04-01 00:00:00.000 2011-04-30 00:00:00.000 2011-05-01 00:00:00.000 2011-05-31 00:00:00.000 2011-06-01 00:00:00.000 2011-06-30 00:00:00.000 2011-07-01 00:00:00.000 2011-07-31 00:00:00.000 2011-08-01 00:00:00.000 2011-08-31 00:00:00.000 2011-09-01 00:00:00.000 2011-09-30 00:00:00.000 2011-10-01 00:00:00.000 2011-10-31 00:00:00.000 2011-11-01 00:00:00.000 2011-11-30 00:00:00.000 2011-12-01 00:00:00.000 2011-12-31 00:00:00.000 */
------解决方案--------------------
http://topic.csdn.net/u/20090906/17/a91cd31e-abf7-4190-8486-c5491aa63c71.html?seed=1380177511&r=59598572#r_59598572
------解决方案--------------------
SQL Server 日期算法 一周的第一天 select @@DATEFIRST 一个月的第一天 select dateadd(mm,datediff(mm,0,getdate()),0) 本周的星期一 select dateadd(wk,datediff(wk,0,getdate()),0) 一年的第一天 SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) 季度的第一天 SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0) 当天的零时 SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0) 上个月的最后一天 :本月第一天减2ms. SELECT dateadd(ms,-2,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)) 本月的最后一天 SELECT dateadd(ms,-2,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)) 本月的第一个星期一 去掉时分秒 DATEADD(day, DATEDIFF(day,0,getdate()), 0) 显示星期几 select datename(weekday,getdate()) 如何取得某个月的天数 SELECT Day(dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))) 判断是否闰年: SELECT case day(dateadd(mm, 2, dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)))) when 28 then '平年' else '闰年' end 一个季度多少天 declare @m tinyint,@time smalldatetime select @m=month(getdate()) select