0

Can someone help me with getting the first and last day of week based on yearweek integer like 201648 without conserning about setting the @@firstdate attribute. I want iso date starting on monday in datetime format.

S3S
  • 24,809
  • 5
  • 26
  • 45
user5767413
  • 175
  • 3
  • 13
  • 2
    Possible duplicate of [Get dates from a week number in T-SQL](http://stackoverflow.com/questions/607817/get-dates-from-a-week-number-in-t-sql) – David Rushton Dec 14 '16 at 16:11

2 Answers2

1

After a little consideration, I thought that perhaps my dynamic Date/Time Range UDF may help here. I use this UDF to generate dynamic date/time ranges. You can supply the desired date range, date part and increment. A tally table would do the trick as well

In this case, we are getting the Nth Monday regardless of the datepart(WK,..) as per the requirements.

Declare @YYYYWW int = 201648

Select WkNbr = B.RetSeq 
      ,WkBeg = B.RetVal
      ,WkEnd = DateAdd(DD,6,B.RetVal)
 From  (
        Select MinDate=Min(RetVal) 
         From  [dbo].[udf-Range-Date](DateFromParts(Left(@YYYYWW,4),1,1),DateFromParts(Left(@YYYYWW,4),1,10),'DD',1) 
         Where DateName(DW,RetVal)='Monday'
       ) A
 Cross Apply (Select * From [dbo].[udf-Range-Date](A.MinDate,DateFromParts(Left(@YYYYWW,4),12,31),'DD',7) ) B
 Where B.RetSeq = Right(@YYYYWW,2)

Returns

WkNbr   WkBeg         WkEnd
48      2016-11-28    2016-12-04

The UDF if interested

CREATE FUNCTION [dbo].[udf-Range-Date] (@R1 datetime,@R2 datetime,@Part varchar(10),@Incr int)
Returns Table
Return (
    with cte0(M)   As (Select 1+Case @Part When 'YY' then DateDiff(YY,@R1,@R2)/@Incr When 'QQ' then DateDiff(QQ,@R1,@R2)/@Incr When 'MM' then DateDiff(MM,@R1,@R2)/@Incr When 'WK' then DateDiff(WK,@R1,@R2)/@Incr When 'DD' then DateDiff(DD,@R1,@R2)/@Incr When 'HH' then DateDiff(HH,@R1,@R2)/@Incr When 'MI' then DateDiff(MI,@R1,@R2)/@Incr When 'SS' then DateDiff(SS,@R1,@R2)/@Incr End),
         cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
         cte2(N)   As (Select Top (Select M from cte0) Row_Number() over (Order By (Select NULL)) From cte1 a, cte1 b, cte1 c, cte1 d, cte1 e, cte1 f, cte1 g, cte1 h ),
         cte3(N,D) As (Select 0,@R1 Union All Select N,Case @Part When 'YY' then DateAdd(YY, N*@Incr, @R1) When 'QQ' then DateAdd(QQ, N*@Incr, @R1) When 'MM' then DateAdd(MM, N*@Incr, @R1) When 'WK' then DateAdd(WK, N*@Incr, @R1) When 'DD' then DateAdd(DD, N*@Incr, @R1) When 'HH' then DateAdd(HH, N*@Incr, @R1) When 'MI' then DateAdd(MI, N*@Incr, @R1) When 'SS' then DateAdd(SS, N*@Incr, @R1) End From cte2 )

    Select RetSeq = N+1
          ,RetVal = D 
     From  cte3,cte0 
     Where D<=@R2
)
/*
Max 100 million observations -- Date Parts YY QQ MM WK DD HH MI SS
Syntax:
Select * from [dbo].[udf-Range-Date]('2016-10-01','2020-10-01','YY',1) 
Select * from [dbo].[udf-Range-Date]('2016-01-01','2017-01-01','MM',1) 
*/
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
1
declare @yrwk int = 201648 
declare @yr int = left(@yrwk,4)
declare @wk int = right(@yrwk,2)

select dateadd (week, @wk, dateadd (year, @yr-1900, 0)) - 4 - datepart(dw, dateadd (week, @wk, dateadd (year, @yr-1900, 0)) - 4) + 1

--returns 11/27/2016 which is Sunday of that week (start of week)
--change +1 to +2 at the end for "Monday"
S3S
  • 24,809
  • 5
  • 26
  • 45
  • 1
    I'll be dipped! Never considered left/right on an INT. Stealing IT +1 for that alone – John Cappelletti Dec 14 '16 at 16:17
  • I can't thank you enough for that little trick. I've twisted myself into knots getting positions of an int. – John Cappelletti Dec 14 '16 at 16:23
  • HAHA no worries @JohnCappelletti. Perhaps it's the simplicity of my mind. – S3S Dec 14 '16 at 16:24
  • @JohnCappelletti don't forget wrapper... it helps to get those middle digits. `SELECT LEFT(RIGHT(123467,4),1)` – S3S Dec 14 '16 at 16:25
  • 1
    This is exactly why I love SO. I learn something new every day. – John Cappelletti Dec 14 '16 at 16:29
  • 1
    @JohnCappelletti Hi, this is simple: The function `LEFT()` expects a string as input. `INT` can be translated to a string implicitly. So this is the same as `LEFT(CAST(SomeInt AS VARCHAR(100),4)`. One must be aware, that the returned result is of string type (but will be implicitly casteable to `INT` again. – Shnugo Dec 14 '16 at 16:34
  • @Shnugo When I saw it in-action, my actual thought was "I'm a dope" – John Cappelletti Dec 14 '16 at 16:37