Log in
::
Register
::
Not logged in
Search:
Home
Articles
Editorials
Forums
Scripts
Blogs
QotD
Books
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Advertise
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2005
»
T-SQL (SS2K5)
»
Function that finds Sundays
34 posts, Page 1 of 4
1
2
3
4
»
»»
Function that finds Sundays
Rate Topic
Display Mode
Topic Options
Author
Message
marty.seed
marty.seed
Posted Friday, November 21, 2008 10:18 AM
Grasshopper
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
Ken Simmons
Ken Simmons
Posted Friday, November 21, 2008 10:31 AM
Ten 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
Lynn Pettis
Lynn Pettis
Posted Friday, November 21, 2008 10:36 AM
SSCrazy 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
Glen Sidelnikov
Glen Sidelnikov
Posted Friday, November 21, 2008 10:45 AM
Old 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
Lynn Pettis
Lynn Pettis
Posted Friday, November 21, 2008 10:57 AM
SSCrazy 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
Lynn Pettis
Lynn Pettis
Posted Friday, November 21, 2008 11:00 AM
SSCrazy 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
marty.seed
marty.seed
Posted Friday, November 21, 2008 11:01 AM
Grasshopper
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
Glen Sidelnikov
Glen Sidelnikov
Posted Friday, November 21, 2008 11:03 AM
Old 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
Lynn Pettis
Lynn Pettis
Posted Friday, November 21, 2008 11:09 AM
SSCrazy 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
Glen Sidelnikov
Glen Sidelnikov
Posted Friday, November 21, 2008 11:11 AM
Old 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 »
34 posts, Page 1 of 4
1
2
3
4
»
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
may
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2009 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use