

SSC Journeyman
Group: General Forum Members
Last Login: Thursday, November 6, 2014 8:13 AM
Points: 96,
Visits: 148


I still prefer the following to get to the first of the month:
SET @dt = DATEADD(d,1DAY(@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)




Ten Centuries
Group: General Forum Members
Last Login: Yesterday @ 1:19 AM
Points: 1,230,
Visits: 2,747


publicdhtech (8/11/2009)
I still prefer the following to get to the first of the month: SET @dt = DATEADD(d,1DAY(@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
 Nigel Useful Links: How to post data/code on a forum to get the best help The "Numbers" or "Tally" Table  Jeff Moden




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 int set @dw = 7  [Sunday  Saturday] == [1  7] declare @d datetime set @d = '7/21/2009' declare @first datetime set @first = DATEADD(d,1DAY(@d),@d) declare @dayofweek int set @dayofweek = DATEPART(dw,@first) print @first + (7+(@dw  @dayofweek))%7




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,1DAY(@dt),@dt) is that when it's used with getdate() the result includes the time as well as the date.




Ten Centuries
Group: General Forum Members
Last Login: Yesterday @ 1:19 AM
Points: 1,230,
Visits: 2,747


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.
 Nigel Useful Links: How to post data/code on a forum to get the best help The "Numbers" or "Tally" Table  Jeff Moden




Ten Centuries
Group: General Forum Members
Last Login: Yesterday @ 1:19 AM
Points: 1,230,
Visits: 2,747





SSC Journeyman
Group: General Forum Members
Last Login: Thursday, November 6, 2014 8:13 AM
Points: 96,
Visits: 148


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 datetime SET @dt = GetDate()  Change to first of the month SET @dt = DATEADD(d,1DAY(@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




Ten Centuries
Group: General Forum Members
Last Login: Yesterday @ 1:19 AM
Points: 1,230,
Visits: 2,747


publicdhtech (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.
David
David,
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
 Nigel Useful Links: How to post data/code on a forum to get the best help The "Numbers" or "Tally" Table  Jeff Moden




SSC Journeyman
Group: General Forum Members
Last Login: Thursday, November 6, 2014 8:13 AM
Points: 96,
Visits: 148


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.




SSCEnthusiastic
Group: General Forum Members
Last Login: Wednesday, May 6, 2015 2:07 PM
Points: 176,
Visits: 283


declare @DayToFind tinyint, @AnyDayInAMonth datetime select @DayToFind = 1, Monday @AnyDayInAMonth = '20090415' Target month
select dateadd(day, (datepart(d, @AnyDayInAMonth)  datepart(dw, @AnyDayInAMonth)  @@datefirst + @DayToFind) % 7, dateadd(day, 1datepart(d, @AnyDayInAMonth), @AnyDayInAMonth)) Seems like a nice singleselect statement to me.



