Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

To Find the First Saturday of any month Expand / Collapse
Author
Message
Posted Tuesday, August 11, 2009 9:36 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC 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,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

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 18, 2014 4:43 AM
Points: 1,184, Visits: 2,680
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


--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #768765
Posted Tuesday, August 11, 2009 10:50 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC 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,1-DAY(@d),@d)

declare @dayofweek int
set @dayofweek = DATEPART(dw,@first)

print @first + (7+(@dw - @dayofweek))%7

Post #768812
Posted Wednesday, August 12, 2009 1:02 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, December 11, 2014 3:53 AM
Points: 292, Visits: 665
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

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 18, 2014 4:43 AM
Points: 1,184, Visits: 2,680
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

Post #769156
Posted Wednesday, August 12, 2009 1:57 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 18, 2014 4:43 AM
Points: 1,184, Visits: 2,680
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


--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #769158
Posted Wednesday, August 12, 2009 8:33 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC 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,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

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, December 18, 2014 4:43 AM
Points: 1,184, Visits: 2,680
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.

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

Post #769374
Posted Wednesday, August 12, 2009 9:08 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC 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.
Post #769387
Posted Wednesday, August 12, 2009 2:15 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, December 5, 2014 11:18 AM
Points: 176, Visits: 281
declare @DayToFind tinyint, @AnyDayInAMonth datetime
select @DayToFind = 1, --Monday
@AnyDayInAMonth = '2009-04-15' --Target month

select 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
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse