Date from week and day on Sql 2012 server

I have the day number of the week, as well as the week number of the year. How can I calculate the date of this day in sql. For example. The number of days on February 22, 2014 is 7th place, and this is the 8th week of the year. Now, how can I calculate the date with this information. He is urgent. Please, help. I need a request.

+4
source share
2 answers

Here is my version

create FUNCTION date_from_week_number_day 
(
     @year int = 2014
    ,@weeknumber int = 8 
    ,@day int = 7


)
RETURNS date
AS
BEGIN


    declare
         @date date
        ,@first_date_of_year date


set @first_date_of_year = CONVERT(date,cast(@year as varchar(4))+'.1.1') --First calculate first day of year


set @date =    dateadd(dd,@day -- we add days acoring of day of week
                        ,dateadd(dd,-1*DATEPART(WEEKDAY,@first_date_of_year) --first day of year is not first day of week so we go back days how day has pass
                            , DATEADD(WEEK,@weeknumber-1,@first_date_of_year) --1. we go to week before last
                            )
                        )

return @date

END

you can call it

SET DATEFIRST  7
select dbo.date_from_week_number_day(2014,8,7)
+1
source

try it

  CREATE FUNCTION dbo.Date_From_WN_DN (@YearNum int,@WeekNum int,@DayNum int)
        RETURNS Date AS
    BEGIN
        DECLARE @FirstDayYear As Date;
        SET @FirstDayYear='01/01/' + CAST(@YearNum As varchar)
        RETURN dateadd(d,((@DayNum)-datepart(weekday,@FirstDayYear)),dateadd(week, @WeekNum-1,@FirstDayYear))
    END


    SET DATEFIRST 7
    SELECT dbo.DEV_VW_WeekSerial (2014,8,7)

O / P :

enter image description here

0
source

Source: https://habr.com/ru/post/1528237/


All Articles