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

 To Find the First Saturday of any month Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, August 11, 2009 9:36 AM
 SSC Journeyman Group: General Forum Members Last Login: Saturday, October 31, 2015 11:35 AM Points: 96, Visits: 150
 I still prefer the following to get to the first of the month:`SET @dt = DATEADD(d,1-DAY(@dt),@dt)`Nigel, nice! Your arithmetic is very crafty and gets a solution in a single statement. But I always prefer code that is easily understood and maintainable by the programmers that come after me, even if there are a couple extra lines and temporary variables.When I am working on a problem I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong.— R. Buckminster Fuller(from Code Complete, 2nd Edition, www.cc2e.com)
Post #768729
 Posted Tuesday, August 11, 2009 9:59 AM
 Ten Centuries Group: General Forum Members Last Login: Thursday, September 29, 2016 7:09 AM Points: 1,276, Visits: 2,820
 publicdh-tech (8/11/2009)I still prefer the following to get to the first of the month:`SET @dt = DATEADD(d,1-DAY(@dt),@dt)`Nigel, nice! Your arithmetic is very crafty and gets a solution in a single statement. But I always prefer code that is easily understood and maintainable by the programmers that come after me, even if there are a couple extra lines and temporary variables.Thanks.A guess a few comments may solve the readability and maintainability issue (sorry didn't have the luxury of time) .The only problem with variables is that you can't have them in a view. I like your method of calculating the first of the month, one minor issue is that it needs to refer to the date twice. Which is ok when that's a simple variable as in your example. It can become a bit hairy when the date itself is a more complex calculation.Nigel
Post #768765
 Posted Tuesday, August 11, 2009 10:50 AM
 SSC Rookie Group: General Forum Members Last Login: Saturday, June 21, 2014 6:47 AM Points: 41, Visits: 70
 Nigel, I think you missed my post on page 1. It has as a similar implementation, and it allows to find the first sunday - saturday of the month. I did not spend time modifying the calculation of the first of the month. Your implementation is nice though.So here is the modified code applying the new first day of month logic`declare @dw intset @dw = 7 -- [Sunday - Saturday] == [1 - 7] declare @d datetimeset @d = '7/21/2009' declare @first datetimeset @first = DATEADD(d,1-DAY(@d),@d) declare @dayofweek intset @dayofweek = DATEPART(dw,@first) print @first + (7+(@dw - @dayofweek))%7`
Post #768812
 Posted Wednesday, August 12, 2009 1:02 AM
 SSC Veteran Group: General Forum Members Last Login: Friday, May 22, 2015 4:49 AM Points: 292, Visits: 675
 One thing to be mindful with - SET @dt = DATEADD(d,1-DAY(@dt),@dt) is that when it's used with getdate() the result includes the time as well as the date.
Post #769142
 Posted Wednesday, August 12, 2009 1:54 AM
 Ten Centuries Group: General Forum Members Last Login: Thursday, September 29, 2016 7:09 AM Points: 1,276, Visits: 2,820
 anand.ramanan (8/11/2009)Nigel, I think you missed my post on page 1. It has as a similar implementation, and it allows to find the first sunday - saturday of the month. I did not spend time modifying the calculation of the first of the month. Your implementation is nice though.Anand,Only problem with your solution is that it relies on DATEFIRST having the default value of 7, as you mentioned in your first post.Although I do like the option to adjust which day to calculate.This can also be done in my solution by adjusting the constant value of 14. ie 15 will give you the first Sunday and 20 the first Friday etc.
Post #769156
 Posted Wednesday, August 12, 2009 1:57 AM
 Ten Centuries Group: General Forum Members Last Login: Thursday, September 29, 2016 7:09 AM Points: 1,276, Visits: 2,820
 wildh (8/12/2009)One thing to be mindful with - SET @dt = DATEADD(d,1-DAY(@dt),@dt) is that when it's used with getdate() the result includes the time as well as the date.Wildh,Good point, I'd overlooked that. Nigel
Post #769158
 Posted Wednesday, August 12, 2009 8:33 AM
 SSC Journeyman Group: General Forum Members Last Login: Saturday, October 31, 2015 11:35 AM Points: 96, Visits: 150
 Nigel/Wildh,In my original post (768063), I convert the result to style 101 (mm/dd/yyyy), so it doesn't matter if @dt has a time or not.`DECLARE @dt datetimeSET @dt = GetDate()-- Change to first of the monthSET @dt = DATEADD(d,1-DAY(@dt),@dt)IF DATEPART(dw,@dt) <> 7 -- If not already Saturday, add a day until it is Saturday WHILE DATEPART(dw, @dt) <> 7 SET @dt = DATEADD(d,1,@dt)PRINT CONVERT(nvarchar(30), @dt, 101)`David
Post #769355
 Posted Wednesday, August 12, 2009 8:53 AM
 Ten Centuries Group: General Forum Members Last Login: Thursday, September 29, 2016 7:09 AM Points: 1,276, Visits: 2,820
 publicdh-tech (8/12/2009)Nigel/Wildh,In my original post (768063), I convert the result to style 101 (mm/dd/yyyy), so it doesn't matter if @dt has a time or not.DavidDavid,That is true in your example. I think, however, Wildh was talking more generally about that method of finding the first of the month.In many cases it may not matter at all that the time component is present, but there will be some when it does - (Sods Law). Nigel
Post #769374
 Posted Wednesday, August 12, 2009 9:08 AM
 SSC Journeyman Group: General Forum Members Last Login: Saturday, October 31, 2015 11:35 AM Points: 96, Visits: 150
 Optimally, in SQL 2008, we would declare @dt as a date type instead of datetime and there would be no time issue. Then we would remove the CONVERT from the PRINT statement and the result would be a true date type instead of varchar.
Post #769387
 Posted Wednesday, August 12, 2009 2:15 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Wednesday, May 6, 2015 2:07 PM Points: 176, Visits: 283
 `declare @DayToFind tinyint, @AnyDayInAMonth datetimeselect @DayToFind = 1, --Monday @AnyDayInAMonth = '2009-04-15' --Target monthselect dateadd(day, (datepart(d, @AnyDayInAMonth) - datepart(dw, @AnyDayInAMonth) - @@datefirst + @DayToFind) % 7, dateadd(day, 1-datepart(d, @AnyDayInAMonth), @AnyDayInAMonth))`Seems like a nice single-select statement to me.
Post #769659

 Permissions