Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Function to get number of days in month Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, April 21, 2011 7:04 PM
 Forum Newbie Group: General Forum Members Last Login: Monday, July 4, 2016 7:54 PM Points: 9, Visits: 448
 Comments posted to this topic are about the item Function to get number of days in month
Post #1097311
 Posted Tuesday, April 26, 2011 11:21 AM
 SSC Veteran Group: General Forum Members Last Login: Thursday, March 17, 2016 7:08 AM Points: 247, Visits: 424
 How is this not better?Select day(dateadd(mm,DateDiff(mm, -1, getdate()),0) -1)
Post #1098842
 Posted Tuesday, April 26, 2011 11:32 AM
 Forum Newbie Group: General Forum Members Last Login: Monday, July 4, 2016 7:54 PM Points: 9, Visits: 448
 Thats clever.
Post #1098853
 Posted Wednesday, April 27, 2011 1:04 AM
 SSC-Addicted Group: General Forum Members Last Login: Sunday, October 9, 2016 3:59 PM Points: 461, Visits: 668
 My personal favourite`SELECT DATEPART(dd, DATEADD(dd, DATEPART(dd, DATEADD(mm, 1, @day)) * -1, DATEADD(mm, 1, @day)))` I suspect you would be hard pressed to notice the difference between any of the above, though the Client Statistics suggests the CASE solution is the most efficient
Post #1099216
 Posted Wednesday, April 27, 2011 2:40 AM
 Right there with Babe Group: General Forum Members Last Login: Monday, May 18, 2015 12:45 AM Points: 790, Visits: 653
 `DECLARE @dtDate DATETIMESET @dtDate = '2011/02/28'SELECT @dtDate= dateadd(mm,datediff(mm,0,@dtDate),0)SELECT datediff(dd,@dtDate,dateadd(mm,1,@dtDate))` Regards,Mitesh OSwal+918698619998
Post #1099246
 Posted Wednesday, April 27, 2011 7:14 AM
 Grasshopper Group: General Forum Members Last Login: Thursday, December 27, 2012 6:32 AM Points: 21, Visits: 97
Post #1099395
 Posted Wednesday, April 27, 2011 8:16 AM
 Ten Centuries Group: General Forum Members Last Login: Thursday, January 31, 2013 8:01 AM Points: 1,228, Visits: 1,046
 Here is some psuedo code for a correct leap year test calculation.if year modulo 400 is 0 then is_leap_yearelse if year modulo 100 is 0 then not_leap_yearelse if year modulo 4 is 0 then is_leap_yearelse not_leap_yearSince this, and other date checking code, is built into the TSQL Date functions I recomend using the examples that have already been posted.
Post #1099487
 Posted Wednesday, April 27, 2011 12:52 PM
 SSC Eights! Group: General Forum Members Last Login: Monday, February 2, 2015 9:35 AM Points: 952, Visits: 553
 Well, since everyone else likes to do subtractions, I thought I'd contribute an addition only solution (if you don't count DATEDIFF as subtraction )`SELECT DATEDIFF(dd,@testdate,(DATEADD(m,1,@testdate)))`
Post #1099730
 Posted Wednesday, April 27, 2011 3:29 PM
 Forum Newbie Group: General Forum Members Last Login: Thursday, July 2, 2015 11:30 AM Points: 8, Visits: 143
 Of course, if you use a calendars table you never have to worry about having to code or validate any date calculations ever again. Sure makes life a lot simpler. Here is how I would get that answer:`SELECT d.DaysInCalendarMonthFROM dbo.dwCalendars dWHERE d.CalendarDate = @SomeDate`
Post #1099855
 Posted Thursday, April 28, 2011 1:36 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, September 5, 2012 2:06 AM Points: 119, Visits: 9
 nice one..
Post #1100018

 Permissions