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


Function to get number of days in month


Function to get number of days in month

Author
Message
Obs
Obs
Mr or Mrs. 500
Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)

Group: General Forum Members
Points: 543 Visits: 424
Well played!

Slope (4/27/2011)
Of course, if you use a calendars table you never have to worry about having to code or validate any date calculations ever again. :-P Sure makes life a lot simpler. Here is how I would get that answer:


SELECT d.DaysInCalendarMonth
FROM dbo.dwCalendars d
WHERE d.CalendarDate = @SomeDate





SanDroid
SanDroid
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3360 Visits: 1046
Obs (4/28/2011)
Well played!

Slope (4/27/2011)
Of course, if you use a calendars table you never have to worry about having to code or validate any date calculations ever again. :-P Sure makes life a lot simpler. Here is how I would get that answer:


SELECT d.DaysInCalendarMonth
FROM dbo.dwCalendars d
WHERE d.CalendarDate = @SomeDate



Only if the Calendars table was created using a LOT differant math than the one presented for this function.
Toby Harman
Toby Harman
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1713 Visits: 670
Back when school was cool (and storage expensive!) they taught us not to store calculated information.

I suspect that all of the functions presented here are lower cost than a read.
MrReddy
MrReddy
SSC Veteran
SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)SSC Veteran (285 reputation)

Group: General Forum Members
Points: 285 Visits: 553
how to give any mnth to the above query?
damir.sovic
damir.sovic
Valued Member
Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)Valued Member (71 reputation)

Group: General Forum Members
Points: 71 Visits: 17
Does anyone noticed this logic:
Month < 8 AND month <> 2 => days_in_month = 30 + (month & 1)
Month >= 8 => days_in month = 30 + ((month+1) & 1)

Of course, there is standard for the February
cyric57-net
cyric57-net
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 17
And why not:

Declare @day DATETIME
Set @day='2011-02-15'

select datediff(day,dateadd(d,1-day(@day),@day),dateadd(m,1,dateadd(d,1-day(@day),@day)))

:-)
Slope
Slope
SSC-Enthusiastic
SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)SSC-Enthusiastic (136 reputation)

Group: General Forum Members
Points: 136 Visits: 143
ramanamreddy (5/2/2011)
how to give any mnth to the above query?


Apologies for the late reply:

SELECT DISTINCT d.DaysInCalendarMonth
FROM dbo.dwCalendars d
WHERE d.CalendarYear = 2011
AND d.CalendarMonthName = 'February'


EITCL
EITCL
Right there with Babe
Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)Right there with Babe (795 reputation)

Group: General Forum Members
Points: 795 Visits: 442
This one doesn't work unless the @testdate is the 1st day of the month. E.g. with @testdate = '2011-01-30' it incorrectly returns 29.
SanDroid
SanDroid
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3360 Visits: 1046
EITCL (6/20/2011)
This one doesn't work unless the @testdate is the 1st day of the month. E.g. with @testdate = '2011-01-30' it incorrectly returns 29.

There are several things wrong with the information in the article.
There are many posts in the discussion thread that show better ways of converting a date.
gwolbr
gwolbr
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 175
If you change the last bit of the case to:

else case when ((Year(@day) % 4 = 0) and ((Year(@day) % 100 != 0) or (Year(@day) % 400 = 0)))

you will perform fewer tests.

With ((A and B) or C), both A and C have to be evaluated even when A is false. When changed to (A and (B or C)), B and C are only evaluated when A is true.
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