SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On


1234»»»

Function that finds Sundays Expand / Collapse
Author
Message
Posted Friday, November 21, 2008 10:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, April 23, 2009 11:52 AM
Points: 24, Visits: 53
Hey there group,

I need to build a function that takes in a date and then returns the corresponding Sunday to that date.

Any ideas?

Marty
Post #606669
Posted Friday, November 21, 2008 10:31 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, July 02, 2009 1:57 PM
Points: 1,081, Visits: 2,314
You can do something like this.

Declare @Date datetime

Set @Date = getdate()

Select @Date -
CASE datename(weekday,@Date)
WHEN 'Monday' THEN 1
WHEN 'Tuesday' THEN 2
WHEN 'Wednesday' THEN 3
WHEN 'Thursday' THEN 4
WHEN 'Friday' THEN 5
WHEN 'Saturday' THEN 6
WHEN 'Sunday' THEN 0
END


Ken Simmons
http://columbusga.sqlpass.org
Post #606687
Posted Friday, November 21, 2008 10:36 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 12:23 PM
Points: 8,079, Visits: 7,671
marty.seed (11/21/2008)
Hey there group,

I need to build a function that takes in a date and then returns the corresponding Sunday to that date.

Any ideas?

Marty


Could you please provide some examples of what your are looking for? I wouldn't know which Sunday date you'd want for a given date just based on your question.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #606694
Posted Friday, November 21, 2008 10:45 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, June 18, 2009 9:02 AM
Points: 371, Visits: 750
select getdate() + 7 - Datepart(dw, getdate())
Post #606703
Posted Friday, November 21, 2008 10:57 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 12:23 PM
Points: 8,079, Visits: 7,671
Glen Sidelnikov (11/21/2008)
select getdate() + 7 - Datepart(dw, getdate())


Given 2008-11-19 which Sunday do you want, 2008-11-16 or 2008-11-23?




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #606718
Posted Friday, November 21, 2008 11:00 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 12:23 PM
Points: 8,079, Visits: 7,671
Glen Sidelnikov (11/21/2008)
select getdate() + 7 - Datepart(dw, getdate())


Also, on my server here at work, the above returns tomorrow (Saturday), not a Sunday.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #606721
Posted Friday, November 21, 2008 11:01 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, April 23, 2009 11:52 AM
Points: 24, Visits: 53
Sorry, good question. The next Sunday

So if I was to pass in todays date I would get 11/23/08
Post #606722
Posted Friday, November 21, 2008 11:03 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, June 18, 2009 9:02 AM
Points: 371, Visits: 750
Given 2008-11-19 which Sunday do you want, 2008-11-16 or 2008-11-23


Lynn, I hope this is not a question addressed to me? I am not providing interpreter's services
I just provided a one line solution which requestor can modify accordingly to the missed part of his/her question.
Use it as an example if it is helpful...
Post #606724
Posted Friday, November 21, 2008 11:09 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 12:23 PM
Points: 8,079, Visits: 7,671
Directed more to the OP, but based on your suggestion, which didn't work on my servers here at work. It returns the Saturday following the given date, not Sunday. But then again, which Sunday does the OP want?




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #606732
Posted Friday, November 21, 2008 11:11 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, June 18, 2009 9:02 AM
Points: 371, Visits: 750
Also, on my server here at work, the above returns tomorrow (Saturday), not a Sunday.


You have to check what are the settings on your server?

select @@datefirst

I believe default is 7 i.e. default first day of the week is Sunday. On mine default is set to 1.
Post #606735
« Prev Topic | Next Topic »

1234»»»

Permissions Expand / Collapse