时间:2021-07-01 10:21:17 帮助过:16人阅读
declare @beginTime datetime, --查询开始时间 @endTime datetime, --查询结束时间 @queryTimeType tinyint; --查询时间类型,1=日,2=周;3=月;4=季;5=年
--查询当天起始时间 set @beginTime = CONVERT(DATETIME,CONVERT(CHAR(10), GETDATE(),120) + ‘ 00:00:00‘,120); --@beginTime:2019-06-25 00:00:00.000 set @endTime = CONVERT(DATETIME,CONVERT(CHAR(10), GETDATE(),120) + ‘ 23:59:59‘,120); --@endTime:2019-06-25 23:59:59.000
--查询本周起始时间 set @beginTime = CONVERT(DATETIME, DATEADD(wk, DATEDIFF(wk,0,DATEADD(dd, -1, getdate()) ), 0) + ‘ 00:00:00‘,120); --@beginTime:2019-06-24 00:00:00.000 set @endTime = CONVERT(DATETIME, DATEADD(wk, DATEDIFF(wk,0,DATEADD(dd, -1, getdate()) ), 6)+ ‘ 23:59:59‘,120); --@endTime:2019-06-30 23:59:59.000
--查询本月起始时间 set @beginTime =CONVERT(datetime, CONVERT(nvarchar(10),dateadd(dd,-day(getdate())+1,getdate()),121)+ ‘ 00:00:00‘,120); --@beginTime:2019-06-01 00:00:00.000 set @endTime = CONVERT(datetime, CONVERT(nvarchar(10),dateadd(dd,-day(getdate()),dateadd(m,1,getdate())),121)+‘ 23:59:59‘,120); --@endTime:2019-06-30 23:59:59.000
--查询本季度起始时间 set @beginTime = CONVERT(datetime,CONVERT(varchar(20),CONVERT(char(8),DATEADD(Month,DATEPART(Quarter,GETDATE())*3-Month(GETDATE())-2,GETDATE()),121)+‘1‘)+‘ 00:00:00‘,120); --@beginTime:2019-04-01 00:00:00.000 set @endTime = CONVERT(datetime, DATEADD(Day,-1,CONVERT(char(8),DATEADD(Month,1+DATEPART(Quarter,GETDATE())*3-Month(GETDATE()),GETDATE()),121)+‘1‘)+‘ 23:59:59‘,120); --@endTime:2019-06-30 23:59:59.000
--查询本年开始结束时间 set @beginTime = CONVERT(datetime,CONVERT(char(5),getdate(),120)+‘01-01 00:00:00‘,120); --@beginTime:2019-01-01 00:00:00.000 set @endTime = CONVERT(datetime,CONVERT(char(5),GETDATE(),120)+‘12-31 23:59:59‘,120); --@endTime:2019-12-31 23:59:59.000
MS SQL Server查询 本日、本周、本月、本季度、本年起始时间
标签:ediff varchar har art nbsp span 声明 family arch