Log in  ::  Register  ::  Not logged in

 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: Thursday, January 16, 2014 2:48 PM Points: 9, Visits: 435
 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: Friday, March 07, 2014 1:46 PM Points: 247, Visits: 419
 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: Thursday, January 16, 2014 2:48 PM Points: 9, Visits: 435
 Thats clever.
Post #1098853
 Posted Wednesday, April 27, 2011 1:04 AM
 Old Hand Group: General Forum Members Last Login: Monday, April 14, 2014 7:46 PM Points: 381, Visits: 535
 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: Yesterday @ 12:00 PM Points: 750, Visits: 581
 `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,232, 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: Friday, October 18, 2013 6:12 AM Points: 951, Visits: 552
 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: Monday, December 09, 2013 10:55 AM Points: 8, Visits: 139
 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 05, 2012 2:06 AM Points: 119, Visits: 9
 nice one..
Post #1100018

 Permissions