Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


get date of first monday of the given year?


get date of first monday of the given year?

Author
Message
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20931 Visits: 9671
ChrisM@Work (8/19/2011)
Ninja's_RGR'us (8/19/2011)
Quite easy with a calendar table.

Select top 1 * FROM dbo.Calendar WHERE Y = 2011 AND DW = <whatever fits your setting> ORDER BY dt.


Just as easy without. Make one for the first week of the year and pick the correct row from it ;-)


Too many good uses to not have 1.


Since my table is less than 2 MB and that I can query the table with an <clustered>index I don't really see the point of trying to go much faster than that. Not saying it's impossible, just never had that need! ;-)
tfifield
tfifield
SSC Eights!
SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)

Group: General Forum Members
Points: 991 Visits: 2890
Ninja's_RGR'us (8/19/2011)
ChrisM@Work (8/19/2011)
Ninja's_RGR'us (8/19/2011)
Quite easy with a calendar table.

Select top 1 * FROM dbo.Calendar WHERE Y = 2011 AND DW = <whatever fits your setting> ORDER BY dt.


Just as easy without. Make one for the first week of the year and pick the correct row from it ;-)


Too many good uses to not have 1.


Since my table is less than 2 MB and that I can query the table with an <clustered>index I don't really see the point of trying to go much faster than that. Not saying it's impossible, just never had that need! ;-)

I agree with Remi on this one. There are just too many uses for calendar tables not to have them. When properly done they give better performance than complicated date math in most cases. You can spell out a month name any way you want in any language and have it displayed correctly. Very simple aggregating and grouping. All sorts of uses.
Todd Fifield
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3254 Visits: 11771
The code to find the first Monday of the year is fairly simple:
First find the 7th day of the year (Jan 7):
dateadd(yy,datediff(yy,0,a.DT),6)

and then find the Monday on or before that date:
dateadd(dd,(datediff(dd,0, JanuarySeventh )/7)*7,0)



Does not depend on any setting of language or datefirst.


select
   DT,
   FirstMonday =
   dateadd(dd,(datediff(dd,0,dateadd(yy,datediff(yy,0,a.DT),6))/7)*7,0)
from
   ( -- Test Data
   select DT = getdate()   union all
   select DT = '20111231'   union all
   select DT = '20121231'   union all
   select DT = '20131231'   union all
   select DT = '20141231'   union all
   select DT = '20110101'   union all
   select DT = '20080229'
   Wink a



Results:
DT                       FirstMonday
------------------------ -----------------------
2011-08-19 14:42:26.310 2011-01-03 00:00:00.000
2011-12-31 00:00:00.000 2011-01-03 00:00:00.000
2012-12-31 00:00:00.000 2012-01-02 00:00:00.000
2013-12-31 00:00:00.000 2013-01-07 00:00:00.000
2014-12-31 00:00:00.000 2014-01-06 00:00:00.000
2011-01-01 00:00:00.000 2011-01-03 00:00:00.000
2008-02-29 00:00:00.000 2008-01-07 00:00:00.000

paul_ramster
paul_ramster
SSC Veteran
SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)SSC Veteran (278 reputation)

Group: General Forum Members
Points: 278 Visits: 534
That's the same algorithm I posted further up the thread, although rather better explained.
The only assumption that is made is that you know a Monday, which in your case is day 0.

I rather unnecessarily tied myself in knots trying to make that assumption explicit and avoid dates before February 1900, to avoid leap year confusions.
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3254 Visits: 11771
paul_ramster (8/23/2011)
That's the same algorithm I posted further up the thread, although rather better explained.
The only assumption that is made is that you know a Monday, which in your case is day 0.

I rather unnecessarily tied myself in knots trying to make that assumption explicit and avoid dates before February 1900, to avoid leap year confusions.


If you are concerned about dates before 1900-01-01, then you could use 1753-01-01, which is the earliest possible datetime and also a Monday. Ask me how I knew that. :-)


Start of Week Function
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47307
Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20931 Visits: 9671
Michael Valentine Jones (8/23/2011)
paul_ramster (8/23/2011)
That's the same algorithm I posted further up the thread, although rather better explained.
The only assumption that is made is that you know a Monday, which in your case is day 0.

I rather unnecessarily tied myself in knots trying to make that assumption explicit and avoid dates before February 1900, to avoid leap year confusions.


If you are concerned about dates before 1900-01-01, then you could use 1753-01-01, which is the earliest possible datetime and also a Monday. Ask me how I knew that. :-)


Start of Week Function
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47307




Real old mother in law?


I know I learned that from our ERP (MS Dynamics / Navision)
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