Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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
 
Old August 14th, 2007, 02:02 AM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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.

 
Old August 14th, 2007, 05:36 AM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default

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
 
Old August 14th, 2007, 05:46 AM
Registered User
 
Join Date: Jul 2007
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

thanks for your help, but how about for different @@DATEFIRST Settings.

Any help will be grateful.

Amlesh

 
Old August 16th, 2007, 04:01 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old August 16th, 2007, 03:42 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Why the long code? There is absolutely no need to involve SET DATEFIRST option setting.

SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, CURRENT_TIMESTAMP), 5)
 
Old August 16th, 2007, 06:58 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Spot on, Peter...

--Jeff Moden
 
Old August 17th, 2007, 03:20 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff!

In-depth explanation of how DATEDIFF works can be found here
http://www.sqlteam.com/article/dated...on-demystified


 
Old August 17th, 2007, 03:42 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old August 17th, 2007, 04:20 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?


 
Old August 17th, 2007, 05:13 AM
Authorized User
 
Join Date: Jun 2003
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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






Similar Threads
Thread Thread Starter Forum Replies Last Post
Show Images from Start Date thru Date istcomnet Classic ASP Basics 2 May 23rd, 2008 07:12 AM
How to find a date range between another date rang tayvonne Access 2 August 3rd, 2006 09:50 AM
copy date values between date controls Alcapone Javascript How-To 1 April 13th, 2006 03:13 AM
DTS Import ( Date string to Date field) gfowajuh SQL Server 2000 1 September 30th, 2003 06:28 AM
Convert String Date to Date for a SQL Query tdaustin Classic ASP Basics 4 July 7th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.