查找SQL Server中当前年份日期给出的上一年的同一天

时间:2014-03-06 11:29:10

标签: sql sql-server date

我正在使用SQL Server,方案是找出截至今天的同一天的上一年日期。

假设 2014-03-06 是今天日期和日期星期四我想在同一周找到前一个谎言中的同一天。 2013年3月7日

任何身体都可以帮忙吗? 这就是我写的:

 DECLARE @DateFrom AS DATETIME
 DECLARE @DateTo AS DATETIME 


SET @DateFrom = '2014-01-01'
SET @DateTo = '2014-02-10'


DECLARE @Count AS INT

SET @Count = DATEDIFF(DAY, @DateFrom, @DateTo)


CREATE TABLE #current_year /*This Year*/
(
  [Date] DATETIME ,
  WeekNum INT,
  [Day] VARCHAR(20),
  Data INT
)
CREATE TABLE #last_year /*This Year -1*/
(
  [Date] DATETIME ,
  WeekNum INT,
  [Day] VARCHAR(20),
  Data INT
)
WHILE ( @Count > 0 ) 
        BEGIN
            INSERT  INTO #current_year
            VALUES  ( CONVERT(VARCHAR(10), @DateFrom, 101),                        
                      DATEPART(week,@DateFrom),
                      DATENAME(weekday, @DateFrom),@Count)          
            INSERT  INTO #last_year
            VALUES  ( CONVERT(VARCHAR(10), DATEADD(YEAR, -1, @DateFrom), 101),                                                  
                      DATEPART(week,DATEADD(YEAR,1,@DateFrom)),
                      DATENAME(weekday, DATEADD(YEAR, -1, @DateFrom)),@Count)  
            SET @DateFrom = DATEADD(day, 1, @DateFrom)
            SET @Count = @Count - 1
        END      
        SELECT * from #current_year     
        SELECT * from #last_year   

        SELECT CONVERT(varchar(10),#current_year.[Date],111) AS CYDate,
               --ISNULL(CONVERT(varchar(10),#last_year.[Date],111) ,/*CONVERT(varchar(10),DateAdd(dd, 1, DATEADD(yy, -1, #current_year.Date)),111)*/) AS LYDate
               --CONVERT(varchar(10),#last_year.[Date],111) AS LYDate
               Coalesce(CONVERT(varchar(10),#last_year.[Date],111) ,DateAdd(dd, 1, DATEADD(yy, -1, #current_year.Date))) AS LYDate,
               #current_year.Data AS CD,
               #last_year.Data AS LD
               FROM #current_year                            
               --LEFT JOIN #last_year ON #last_year.WeekNum = #current_year.WeekNum
   --                                 AND #last_year.[Day] = #current_year.[Day]
                                    Left JOIN #last_year ON #last_year.WeekNum =  DatePart(wk, GETDATE())



DROP TABLE #current_year
DROP TABLE #last_year

以下是输出:Image

以下是添加解决方案后的输出,现在在左连接中排除了上一年的(NULL)数据

4 个答案:

答案 0 :(得分:5)

基本上你需要找到这个和前几年相同日期之间的天数差异,然后通过mod 7理解“天差”,并将其与上一年的日期相加:

DECLARE @now DATETIME
SET @now = '2014-03-06'


SELECT CAST (DATEADD(YEAR, -1, @now) + (CAST (@now as INT) - CAST (DATEADD(YEAR, -1, @now) AS INT)) % 7 AS DATE)

返回

2013-03-07

答案 1 :(得分:2)

尝试

DECLARE @now Date
SET @now = '2014-06-03'
SELECT DATEADD(week, -52, @now)

答案 2 :(得分:1)

SELECT DateName(dw, DATEADD(yy, -1, GETDATE()))

给出Wednesday

SELECT DateName(dw, DateAdd(dd, 1, DATEADD(yy, -1, GETDATE())))

给出Thursday

编辑

SELECT DateAdd(dd, 1, DATEADD(yy, -1, GETDATE()))

给出'2013-03-07 17:30:16.590'

您需要根据您的要求投射日期..

<强>更新 用,改变这部分,

Left JOIN #last_year ON #last_year.WeekNum =  DatePart(wk, GETDATE())
在你的情况下

Left JOIN #last_year ON DatePart(wk,  #last_year.[Date]) =  DatePart(wk, #current_year.[Date])

更新2:

Left JOIN #last_year ON (MONTH(#last_year.[Date])=MONTH(#current_year.[Date]) and Day(#last_year.[Date])=Day(#current_year.[Date])) 

输出

Update output

<强>输出:

Left JOIN #last_year ON (#last_year.WeekNum = #current_year.WeekNum and #last_year.[Day] = #current_year.[Day])

output 2     
选择哪个是您所需的输出。

答案 3 :(得分:0)

DECLARE @Date DATE = '2014-03-06'
SELECT DATEADD(WEEK,-52,@Date)

返回值:

2013-03-07