从SQL Server 2012

时间:2016-03-02 07:00:53

标签: sql-server sql-server-2012

感谢所有人阅读我的问题,通过提供年份,周数和日期名称,从SQL Server 2012中检索日期已经成为一个大问题。

假设我有

Year = 2016
Week number = 1
Day Name ='FRI'
First day of week='SUN'

预期结果:

01-01-2016

我该怎么做?

编辑:我从here找到了类似的解决方案,但我没有月份名称。

3 个答案:

答案 0 :(得分:1)

我的建议是基于您提供的链接中的问题解决方案。 基本上,我创建了一个日历,其中包含自今年1月1日以来的日期,然后是@x周,然后查询该日历:

-- provided data:
DECLARE @Year int = 2016,
        @WeekNumber int = 1,
        @DayName char(3) = 'Fri';

-- Calculate start date and end date
DECLARE @StartDate date, 
        @EndDate date;

SELECT  @StartDate = CAST('01-01-'+ CAST(@Year as char(4)) as date),
        @EndDate = DATEADD(WEEK, @WeekNumber, @StartDate)

-- Create the calendar
;WITH CTE AS 
(
    SELECT @StartDate as TheDate
    UNION ALL
    SELECT DATEADD(DAY, 1, TheDate)
    FROM CTE 
    WHERE DATEADD(DAY, 1, TheDate) <= @EndDate
)

-- Finally, query the calendar:
SELECT  TheDate
FROM CTE
WHERE DATEPART(WEEK, TheDate) = @WeekNumber 
AND YEAR(TheDate) = @Year 
AND DATENAME(WEEKDAY, TheDate) LIKE @DayName + '%'
OPTION(MAXRECURSION 0)

结果:

TheDate
----------
2016-01-01

注意:如果您指定的日期是星期一,此解决方案将不返回任何行,因为2016年的第一周从星期五开始。

答案 1 :(得分:1)

尝试这个

   DECLARE @Year        varchar(4)
DECLARE @WeekDayday  varchar(10) 
DECLARE @WeekNumber  int        
SET @Year        ='2016'  
SET @WeekDayday  ='fri'   
SET @WeekNumber  =1


--used to solve
DECLARE @StartDate datetime
       ,@EndDate   datetime
       ,@FirstWeek int

SET @StartDate='01-01-'+' '+@Year
SET @EndDate=@StartDate+38
SET @FirstWeek=DATENAME(week,@StartDate)-1

;with AllDates AS
(
    SELECT @StartDate AS DateOf, DATENAME(week,@StartDate)-@FirstWeek AS WeekOf, DATENAME(weekday,@StartDate) AS WeekDayOf
    UNION ALL
    SELECT DateOf+1, DATENAME(week,DateOf+1)-@FirstWeek AS WeekOf, DATENAME(weekday,DateOf+1) AS WeekDayOf
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT
    DateOf
    FROM AllDates
    WHERE WeekOf=@WeekNumber AND WeekDayOf LIKE @WeekDayday+'%'
    ORDER BY DateOf

答案 2 :(得分:0)

如何通过按周编号评估月份名称来改进that答案:

--given info
DECLARE @Year        varchar(4);
DECLARE @MonthName   varchar(10);
DECLARE @WeekDayday  varchar(10);
DECLARE @WeekNumber  int;

SET @Year        = '2016';
SET @WeekDayday  = 'Tue';  
set @WeekNumber  = 46;

-- get month number by week
declare @w int = 0;
declare @m int = 1;
while (@w <= @WeekNumber and @m < 13) begin
  set @w = datepart(wk, datefromparts(@year, @m, 1));
  if (@w <= @WeekNumber and @m < 13) begin
    set @m = @m + 1;
  end;
end;
set @m = @m -1;

-- get month name
set @MonthName = left(DateName(month ,DateAdd(month ,@m ,0 ) - 1), 3);


--used to solve
DECLARE @StartDate datetime
       ,@EndDate   datetime
       ,@FirstWeek int

SET @StartDate='01 '+@MonthName+' '+@Year
SET @EndDate=@StartDate+38
SET @FirstWeek=DATENAME(week,@StartDate)-1


;with AllDates AS
(
    SELECT @StartDate AS DateOf, DATENAME(week,@StartDate)-@FirstWeek AS WeekOf, DATENAME(weekday,@StartDate) AS WeekDayOf
    UNION ALL
    SELECT DateOf+1, DATENAME(week,DateOf+1)-@FirstWeek AS WeekOf, DATENAME(weekday,DateOf+1) AS WeekDayOf
        FROM AllDates
    WHERE DateOf<@EndDate
)
SELECT
    DateOf ,WeekOf ,WeekDayOf
    FROM AllDates
    WHERE datepart(wk, DateOf) = @WeekNumber AND WeekDayOf LIKE @WeekDayday+'%'
    ORDER BY DateOf