## Tally Calendars and 'Week 1'

 Author Message dwain.c SSCrazy Eights Group: General Forum Members Points: 9513 Visits: 6431 Jason-299789 (1/8/2013)Final Edit, Dwain, I figured out why yours didnt work you're addind on the Julian day which is 1-365/366 I think you need to add the Week day number something like this`CREATE TABLE #Calendar ([Date] DATETIME PRIMARY KEY ,[Year] INT ,[JulWk] INT ,[WkDNo] INT ,ISO_WK AS (CAST([Year] AS CHAR(4)) + 'W' + CAST([JulWk] AS VARCHAR(2)) + '-' + CAST([WkDNo] AS VARCHAR(3))) )INSERT INTO #CalendarSELECT [Date], [Year], [JulWk], [WkDNo]FROM GenerateCalendar('20120101', 365*2)SELECT * FROM #CalendarWHERE ISO_WK LIKE '2012W26-[67]'`Ah-ha! Indeed that does seem to fix it. Thought Joe meant Julian day not week day number. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables