SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Tally Calendars and 'Week 1'


Tally Calendars and 'Week 1'

Author
Message
dwain.c
dwain.c
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28493 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 #Calendar
SELECT [Date], [Year], [JulWk], [WkDNo]
FROM GenerateCalendar('20120101', 365*2)

SELECT * FROM #Calendar
WHERE 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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search