blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Jim 2.0

PL/SQL and T-SQL for Week Ending Date

Recently my colleague challenged me to  find a method for identifying the week ending date for the week which any given date belongs to; i.e. say for a gven date which happens to be a Tuesday, what is the date of the end of the week? In this example, week ending is defined as a Sunday, however depending on how your server is set up, week ending may be considered the Saturday (or more accurately week commencing may either be a Sunday or a Monday), but we easily deal with this as you will see.

 T-SQL

 The two methods are actually quite different as Oracle has some handy functions to help us, which T-SQL does not, therefore this method simply involves a calculation based on knowing which day of the week a given day is, and how many days are in a week, therefore how many days until the end of the week.

select   cast(CONVERT(char(10), getdate(), 110) as datetime) [today]

,        datename(dw, getdate()) [day]

,        datepart(dw, getdate()-1) [day of week]

,        datepart(dw, getdate()-1)-1 [days since monday]

,        7-datepart(dw, getdate()-1) [days until sunday]

,        cast(CONVERT(char(10), (getdate()-(datepart(dw, getdate()-1)-1)), 110) as datetime) [monday]

,        cast(CONVERT(char(10), (getdate()+(7-datepart(dw, getdate()-1))), 110) as datetime) [sunday]

The first line simply identifies the date in question, which for this example is the current date, cast to remove the time portion, then we can use the datepart and datename functions to identify further details about this date.

After identifying the name of the day, and day of the week (note that my server is configured to take Sunday as the first day of the week, but I want it to be Monday in this scenario, therefore I subtract 1 to make Monday the first day) it is relatively straightforward to calculate how many days since Monday and until Sunday. Days since is defined as 'day of week'-1, because Monday is the first day of the week; Days until is defined as 7-'day of week', because Sunday is the seventh day of the week.

Using these two numbers we can then either subtract or add them from the given date, which is then cast as a date to given the dates of the beginning and end of the week in question.

 PL/SQL

In Oracle the task is much simpler because we can make use of the next_day function to  identify the date of a subsequent weekday for a given date.

select   trunc(sysdate) "Today"

,        next_day(trunc(sysdate-7), 'monday') "Monday"

,        next_day(trunc(sysdate), 'sunday') "Sunday"

from     dual

;

This makes identifying the next Sunday very easy as can be seen, and whilst for some reason there isn't a corresponding 'previous day' function, by subtracting 7 from our date and then finding the next Monday, we can find the date of the previous Monday without too much trouble.

Now that we have these methods, they can be incorporated into my previous entries for date range lookups for bothe PL/SQL and T-SQL

James

Published 24 April 2007 15:01 by James.Pipe

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Jim 2.0 said:

After experimentation with various datepart and datename methods following my most recent blog post ,

April 24, 2007 15:59
 

anusha said:

i need some tutorial for geting grip on sql server(tsql)

can u provide me any links to learn tsql

November 13, 2007 11:09
 

James.Pipe said:

November 14, 2007 17:00
 

shalini said:

i need different date formats

November 21, 2007 10:01
 

James.Pipe said:

For T-SQL you can use the cast(convert()) to format the date in whichever way you need. See books online for a list of available formats:

http://msdn2.microsoft.com/en-us/library/ms187928.aspx

In PL/SQL, use to_char to change the output format of a date; for example:

to_char(trunc(sysdate), 'DD-MM-YYYY')

Hope that helps

November 21, 2007 14:43
 

Aysh said:

given any date i need to find the day it belongs.for eg:given 17/01/1985,i need to g find the day on which it falls.Cn u help me out with this

November 22, 2007 16:23
 

James.Pipe said:

November 23, 2007 18:31
 

tsql day of week said:

May 29, 2008 14:39
 

Patrick Kuras said:

Here's a T-SQL function I wrote which does basically the same thing, but for any day of the week you choose to be the ending day. You pass the date of interest, and an integer representing the day of the week that is your "end day" (Sunday is 1), and you get back a date that is the next "week ending" day.

ALTER FUNCTION [dbo].[weekEnding]

(

@theDate datetime,

@theEndDay int

)

RETURNS datetime

AS

BEGIN

DECLARE @theWeekEndingDate datetime

DECLARE @theDateOnly datetime

set @theDateOnly =

(

cast(CONVERT(char(10), @theDate, 110) as datetime)

)

set @theWeekEndingDate =

(

case

when DATEPART(dw, @theDateOnly) <= @theEndDay then

DATEADD(dd, @theEndDay - DATEPART(dw, @theDateOnly), @theDateOnly)

when DATEPART(dw, @theDateOnly) > @theEndDay then

DATEADD(dd, 7 + @theEndDay - DATEPART(dw, @theDateOnly), @theDateOnly)

end

)

RETURN @theWeekEndingDate

END

May 30, 2008 22:17

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server (Personal Edition), by Telligent Systems