 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 14th, 2007, 02:02 AM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
get the coming saturday date of a given date
Hi all,
i getting problem to get the coming saturday date of a given date.
My real problem is that i want it for all @@DATEFIRST Settings.
Plz help.
Thanks.
|
|

August 14th, 2007, 05:36 AM
|
|
Friend of Wrox
|
|
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The logic is that use Datename(dw,'your_date'). Now this will return the day like monday,tuesday, etc. Accordingly, you can add the number of days into it like if it returns monday, Add 5 to the date as: Dateadd(dd,5,'your_date'). You can also use Case in the query. And you will also have to check that date should never cross 28, 30 or 31 depending on the month. If so happens, you need to update the month too and in case of december might be year also.
Gaurav
|
|

August 14th, 2007, 05:46 AM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
thanks for your help, but how about for different @@DATEFIRST Settings.
Any help will be grateful.
Amlesh
|
|

August 16th, 2007, 04:01 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi.
The following code will return the next saturday for any @@DATEFIRST settings. If the given date IS a saturday, then it will return the saturday a week later. There is no need to adjust for number of days in month or the crossing of a year-boundary.
Code:
DECLARE @YourDate datetime
SET @YourDate = '20070803'
DECLARE @NextSaturday datetime
DECLARE @OrigDateFirst int
SET @OrigDateFirst = @@DATEFIRST --Save original @@DATEFIRST value
SET DATEFIRST 6 --saturday as first day of week
SET @NextSaturday = DATEADD(day,8-DATEPART(dw,@YourDate),@YourDate)
SET DATEFIRST @OrigDateFirst --Restore @@DATEFIRST
SELECT @NextSaturday
Gert
|
|

August 16th, 2007, 03:42 PM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Why the long code? There is absolutely no need to involve SET DATEFIRST option setting.
SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP), 5)
|
|

August 16th, 2007, 06:58 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Spot on, Peter...
--Jeff Moden
|
|

August 17th, 2007, 03:42 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hmm, I must admit that your solution was a bit more clever than mine, Peter, but since the OP was grateful for ANY help (and no one had given it), I went for the solution I could come up with at the moment.. hence the long code.
But all praise to you.. :)
Gert
|
|

August 17th, 2007, 04:20 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I just objected to changing the DATEFIRST setting forth and back.
If the situtation were reversed, wouldn't you like a solution that was as simple as possible?
|
|

August 17th, 2007, 05:13 AM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes indeed, and I have no objection to your objection :)
I really did like your solution better, so the praising was well meant... no ulterior motive behind that.
Keep up the good work.
Gert
|
|
 |