Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 14th, 2007, 02:02 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old August 14th, 2007, 05:36 AM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
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
Reply With Quote
  #3 (permalink)  
Old August 14th, 2007, 05:46 AM
Registered User
 
Join Date: Jul 2007
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old August 16th, 2007, 04:01 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
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

Reply With Quote
  #5 (permalink)  
Old August 16th, 2007, 03:42 PM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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)
Reply With Quote
  #6 (permalink)  
Old August 16th, 2007, 06:58 PM
Friend of Wrox
Points: 1,536, Level: 15
Points: 1,536, Level: 15 Points: 1,536, Level: 15 Points: 1,536, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , MI, USA.
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Spot on, Peter...

--Jeff Moden
Reply With Quote
  #7 (permalink)  
Old August 17th, 2007, 03:20 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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


Reply With Quote
  #8 (permalink)  
Old August 17th, 2007, 03:42 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
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

Reply With Quote
  #9 (permalink)  
Old August 17th, 2007, 04:20 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
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?


Reply With Quote
  #10 (permalink)  
Old August 17th, 2007, 05:13 AM
Authorized User
 
Join Date: Jun 2003
Location: , , Norway.
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:43 AM.


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