View previous topic :: View next topic |
Author |
Message |
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Mon Mar 13, 2006 6:02 pm Post subject: How to Retrieve Last Saturday Date using SQL ? |
|
|
Hi,
Does anybody have a one-pass solution without programming a whole lot around it to get the previous Saturday date of the week given any date as input.
For example if the Input is TODAYS DATE : 03-13-06 or tomorrows or day after tomorrow date the Output still should be 03-11-06 (last saturday date).
Thnks.
Vini |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Mon Mar 13, 2006 7:03 pm Post subject: |
|
|
vini,
Try this untested sql
with current date
Code: |
SELECT DATE(CURRENT DATE) - DAYOFWEEK(CURRENT DATE) DAYS
FROM SYSIBM.SYSDUMMY1;
|
with any date
Code: |
SELECT DATE('2006-03-15') - DAYOFWEEK('2006-03-15') DAYS
FROM SYSIBM.SYSDUMMY1;
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Tue Mar 14, 2006 11:31 am Post subject: |
|
|
Awesome !!! Tested for several dates and it works fine
Kolusu,
Can you explain how it evaluates/works ? Thnks.
|
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Tue Mar 14, 2006 11:46 am Post subject: |
|
|
Quote: |
Kolusu, Can you explain how it evaluates/works ? Thnks.
|
Vini,
It is very simple. To get a previous date all we need to do is substract 'n' of days. we use that simple logic here.
ex: today is tuesday. so if you want last saturday's date all you need to do is subtract 3 days from today. The number of days to subtract is got by using the scalar function DAYOFWEEK.
The DAYOFWEEK function returns an integer in the range of 1 to 7 that represents the day of the week where 1 is Sunday and 7 is Saturday.
Check this link for a detailed explanation of the function
http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNSQH11/3.2.20?SHELF=&DT=20010718164132&CASE=
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Tue Mar 14, 2006 1:21 pm Post subject: |
|
|
Kolusu,
I am confused because when I brake up the suggested SQL to understand it ...as follows..I get a -171 Sql Cd error .
SELECT DATE(CURRENT DATE) - DAYOFWEEK(CURRENT DATE) From SysIbm.Sysdummy1.
Hence, I do not understand how appending a DAYS clause to the above does it all ?
I have used the DAYOFWEEK before so that was not the source of confusion.
I dont really need to understand to make it work , that I have tested and it sure does , but when we have a formal code review I could be questioned and I just wanted to be able to handle peer review questions.
Thanks
Vini |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Tue Mar 14, 2006 1:38 pm Post subject: |
|
|
Quote: |
I am confused because when I brake up the suggested SQL to understand it ...as follows..I get a -171 Sql Cd error .
SELECT DATE(CURRENT DATE) - DAYOFWEEK(CURRENT DATE) From SysIbm.Sysdummy1.
Hence, I do not understand how appending a DAYS clause to the above does it all ?
|
Vini,
The reason you got that error is because DB2 does not know what to subtract. Is it days or months or years. when you are performing Date arthimetic and if one of the operand is a date, the other operand must be a date duration or labeled duration of years, months, or days. Once you specify the keywords DAYS then it automatically knows exactly how to perform the date arthimetic.
Quote: |
I dont really need to understand to make it work , that I have tested and it sure does , but when we have a formal code review I could be questioned and I just wanted to be able to handle peer review questions.
|
Wrong approach. You need to understand the logic behind the code. You will be a better person to fix it even if for somereason it breaks. IMHO do not code something which you do not understand.
As such it is not that complicated logic to understand. Check this link to understand more about Datetime arthimetic
http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/BOOKS/DSNSQH11/2.20.9?SHELF=&DT=20010718164132&CASE=
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Tue Mar 14, 2006 2:39 pm Post subject: |
|
|
Kolusu,
If I was the kinds to code without understanding I wouldnt have bothered to ask for an explanation behind the logic. Rest assured I will understand it sooner or later..there still is time before the code moves to prodn.
Although I still do not understand because what you are doing is subtracting a date from itself (in days) and arriving at a previous saturday date.
I realise the mistake I made in the break up SQL and the following gets the same results ..as am operating on 2 dates of same format now:
SELECT DATE(CURRENT DATE) - DATE(DAYOFWEEK(CURRENT DATE))
FROM SYSIBM.SYSDUMMY1;
What if one needs to get a WEDNESDAY day instead of SATURDAY ..using the same logic ...how would this simple logic work that out ? Perhaps if there are 2 SQL to compare it may be easier to understand.
Can anyone else chime in .. with an alternate view point or reasoning ..if you have one ? I maybe really dumb to be still asking but am not pretending here.
Thanks. |
|
Back to top |
|
|
Bithead Advanced
Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Tue Mar 14, 2006 3:05 pm Post subject: |
|
|
This is how this works:
SELECT DATE(CURRENT DATE) - DAYOFWEEK(CURRENT DATE) DAYS
If today is a Sunday, then DAYOFWEEK(CURRENT DATE) returns a value of 1 so
SELECT DATE(CURRENT DATE) - 1 DAYS is yesterday which is Saturday.
If today is a Monday, then DAYOFWEEK(CURRENT DATE) returns a value of 2 so
SELECT DATE(CURRENT DATE) - 2 DAYS is 2 days ago which is Saturday.
If today is a Wednesday, then DAYOFWEEK(CURRENT DATE) returns a value of 3 so
SELECT DATE(CURRENT DATE) - 2 DAYS is 3 days ago which is Saturday.
and so on up to
If today is a Saturday, then DAYOFWEEK(CURRENT DATE) returns a value of 7 so
SELECT DATE(CURRENT DATE) - 7 DAYS is a week ago which is last Saturday. |
|
Back to top |
|
|
SureshKumar Intermediate
Joined: 23 Jan 2003 Posts: 211 Topics: 21
|
Posted: Tue Mar 14, 2006 3:17 pm Post subject: |
|
|
vini,
Quote: |
Does anybody have a one-pass solution without programming a whole lot around it to get the previous Saturday date of the week given any date as input.
|
you had a specific requirement and a simple solution was provided. Look into how DAYOFWEEK functions (1 thru 7), you can tweak the sql to work for a different day, then again there can be different solution for a different request. Saturday is simple as the da of week starts from Sunday(which is 1). thanks |
|
Back to top |
|
|
kolusu Site Admin
Joined: 26 Nov 2002 Posts: 12375 Topics: 75 Location: San Jose
|
Posted: Tue Mar 14, 2006 3:25 pm Post subject: |
|
|
Quote: |
What if one needs to get a WEDNESDAY day instead of SATURDAY ..using the same logic ...how would this simple logic work that out ? Perhaps if there are 2 SQL to compare it may be easier to understand.
Can anyone else chime in .. with an alternate view point or reasoning ..if you have one ? I maybe really dumb to be still asking but am not pretending here.
|
Vini,
The calculation for last saturday was easy because the week starts from sunday, so if you essentially subtract the no: of days then you will get the last saturday as you can see from bithead's explanation.
Now if you want last wednesday's date then it gets trickier. In that case we use the case statement to modify the no: of days to be subtracted.
try this
Code: |
SELECT DATE(CURRENT DATE) - (CASE DAYOFWEEK(CURRENT DATE)
WHEN 1 THEN 4
WHEN 2 THEN 5
WHEN 3 THEN 6
WHEN 4 THEN 7
WHEN 5 THEN 1
WHEN 6 THEN 2
WHEN 7 THEN 3
END) DAYS
FROM SYSIBM.SYSDUMMY1;
|
Code: |
SELECT DATE('2006-03-14') - (CASE DAYOFWEEK('2006-03-14')
WHEN 1 THEN 4
WHEN 2 THEN 5
WHEN 3 THEN 6
WHEN 4 THEN 7
WHEN 5 THEN 1
WHEN 6 THEN 2
WHEN 7 THEN 3
END) DAYS
FROM SYSIBM.SYSDUMMY1;
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
|
Bithead Advanced
Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Tue Mar 14, 2006 4:36 pm Post subject: |
|
|
This seems to work to get last wednesday's date though I have not fully tested it:
Code: |
SELECT CURRENT DATE
- MOD(DAYOFWEEK(CURRENT DATE) + 3, 8) DAYS
FROM SYSIBM.SYSDUMMY1
|
Substitute the +3 for other days as follows:
Sunday +6
Monday +5
Tuesday +4
down to
Saturday +0
Let me know how this works out. |
|
Back to top |
|
|
vini Intermediate
Joined: 12 Jan 2004 Posts: 240 Topics: 48 Location: Maryland
|
Posted: Wed Mar 15, 2006 9:33 am Post subject: |
|
|
Thanks Bithead , It all makes sense now and your last SQL sure does work too |
|
Back to top |
|
|
|
|