本文介绍了SQL Server:date自动化TSQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我希望SQL查询自动获取日期 请注意:年应始终从 1月的最后一个星期日开始。 / p> 我想要一个SQL查询下面的例子;假设当前日期为'2017-01-01' SQL应选择以下日期: $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ >(年初(1月的最后一个星期日)'和'上周的最后一天(星期六)') 我有这个查询:在 之间 当$ DatePart(w,DateAdd(d,30,DATEADD(y, - DatePart y,GetDate())+ = 1 然后DateAdd(d,30,DATEADD(y, - DatePart(y,GetDate())+ 1,convert(date,GetDate ))) Else DateAdd(d,7 - DatePart(w,DateAdd(d,30,DATEADD(y, - DatePart(y,GetDate())+ 1,GetDate() DateAdd(d,30,DATEADD(y, - DatePart(y,GetDate())+ 1,convert(date,GetDate()))) end 和 convert(date,dateadd(wk,datediff(wk,0,getdate()) - 1,0)+ 5) 上述查询返回错误的res 示例1 :错误 假设当前日期为2017-01-01,SQL查询返回以下日期: '2017-02-05之间'和'2016-12-31' 哪些错误。 示例2 :正确 假设当前日期是'2017-02-12 ',那么SQL查询返回这些日期: '2017-02-05'和'2017-02-11'之间 哪些是正确的日期 - OK。 当前的日期是1月份,问题总是存在 如何修复示例1请任何更新的SQL查询?解决方案如果任何一年的二月是您的固定参数,那么您将当前日期与之对比,并基于结果使用与否: DECLARE @GetMyDate datetime ='2017-01-01' - GETDATE() SELECT @GetMyDate, CASE WHEN @GetMyDate< DATEADD(Day,-1,CONVERT(datetime,CONVERT(varchar(4),(year(@GetMyDate)))+'-02-01')) THEN DATEADD(Day,-1,CONVERT ,CONVERT(varchar(4),(year(@GetMyDate)-1))+'-02-01')) ELSE DateAdd(d,7 - DatePart(w,DateAdd(d,30,DATEADD y, - DatePart(y,@GetMyDate)+ 1,@GetMyDate)))+ 1,DateAdd(d,30,DATEADD(y, - DatePart(y,@GetMyDate)+ 1,convert(date,@GetMyDate) ) END AS StartDate convert(date,dateadd(wk,datediff(wk,0,@GetMyDate) - 1,0)+ 5)AS EndDate 更新: BETWEEN CASE WHEN GETDATE()< = DATEADD(Day,-0,CONVERT(datetime,CONVERT(varchar(4),(year(GETDATE()))+'-02-01')) THEN DATEADD(日,-0,CONVERT(datetime,CONVERT(varchar(4),(year(GETDATE()) - 1))+'-02-01')) ELSE DateAdd(d,7 - DatePart (w,DateAdd(d,30,DATEADD(y, - DatePart(y,GETDATE())+ 1,GET DATE())))+ 1,DateAdd(d,30,DATEADD(y, - DatePart(y,GETDATE())+ 1,convert(date,GETDATE()))) END AND CASE WHEN GETDATE()= DATEADD(Day,-0,CONVERT(datetime,CONVERT(varchar(4),(year(GETDATE()))+'-02-01' ) THEN DATEADD(Day,-1,CONVERT(datetime,CONVERT(varchar(4),(year(GETDATE()))+'-02-01')) ELSE Con​​vert date,dateadd(wk,datediff(wk,0,GETDATE()) - 1,0)+ 5) END I want SQL query to get the dates automatically,Please Note: year should always start on the Last Sunday of January.I want a SQL query for below example; assume current date is '2017-01-01'SQL should pick these dates:Between '2016-01-31' and '2016-12-31'(between 'Start of the Year (Last Sunday of January Month)' and 'Last day (Saturday) of previous week')I have this query:Between case When DatePart(w, DateAdd(d, 30, DATEADD(y, - DatePart(y, GetDate()) + 1, GetDate()))) = 1 Then DateAdd(d, 30, DATEADD(y, - DatePart(y, GetDate()) + 1, convert(date, GetDate()))) Else DateAdd(d, 7 - DatePart(w, DateAdd(d, 30, DATEADD(y, - DatePart(y, GetDate()) + 1, GetDate()))) + 1, DateAdd(d, 30, DATEADD(y, - DatePart(y, GetDate()) + 1, convert(date, GetDate()))) ) endand convert(date, dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) + 5) The above query is returning wrong results:Example 1: WrongAssume current date is '2017-01-01', the SQL query returns these dates:Between '2017-02-05' and '2016-12-31'which are wrong.Example 2: CorrectAssume current date is '2017-02-12', then the SQL query returns these dates:Between '2017-02-05' and '2017-02-11'which are the correct dates - OK. The problem is always when the current date is in JanuaryHow to fix example 1 please? Any updated SQL query? 解决方案 If Feb of any year is your fixed parameter then you compare the current date to it and based on the result use it or not: DECLARE @GetMyDate datetime = '2017-01-01'--GETDATE() SELECT @GetMyDate, CASE WHEN @GetMyDate < DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(@GetMyDate))) + '-02-01')) THEN DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(@GetMyDate)-1)) + '-02-01')) ELSE DateAdd(d, 7 - DatePart(w, DateAdd(d, 30, DATEADD(y, - DatePart(y, @GetMyDate) + 1, @GetMyDate))) + 1, DateAdd(d, 30, DATEADD(y, - DatePart(y, @GetMyDate) + 1, convert(date, @GetMyDate)))) END AS StartDate, convert(date, dateadd(wk, datediff(wk, 0, @GetMyDate) - 1, 0) + 5) AS EndDateUpdate:BETWEEN CASE WHEN GETDATE() <= DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01')) THEN DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE())-1)) + '-02-01')) ELSE DateAdd(d, 7 - DatePart(w, DateAdd(d, 30, DATEADD(y, - DatePart(y, GETDATE()) + 1, GETDATE()))) + 1, DateAdd(d, 30, DATEADD(y, - DatePart(y, GETDATE()) + 1, convert(date, GETDATE())))) END AND CASE WHEN GETDATE() = DATEADD(Day,-0,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01')) THEN DATEADD(Day,-1,CONVERT(datetime, CONVERT(varchar(4), (year(GETDATE()))) + '-02-01')) ELSE Convert(date, dateadd(wk, datediff(wk, 0, GETDATE()) - 1, 0) + 5) END 这篇关于SQL Server:date自动化TSQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 09:40