If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Form a date based on month, day of week and day of month

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-10, 18:39
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
Form a date based on month, day of week and day of month

Question -- how can you form the date 5/31/2010 based on this information ?

Month = 5
DayOfWeek = 2
Week of Month = 5


Thanks!
Reply With Quote
  #2 (permalink)  
Old 08-12-10, 22:58
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Unless you know the year, you cannot. If you do know the year, then you can proceed using the first of the month as a base.

Andy
Reply With Quote
  #3 (permalink)  
Old 08-13-10, 00:06
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
thanks.. yes I forgot to mention that.. the year is given too..
Reply With Quote
  #4 (permalink)  
Old 08-13-10, 09:23
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
This may be an answer.
But, it is rather complex expressions.
So, I want to find simpler expression.

Code:
------------------------------ Commands Entered ------------------------------
SELECT year_ , month_ , dayofweek , weekofmonth
     , (SELECT firstday + ( weekofmonth * 7 - MOD( DAYOFWEEK(firstday) - dayofweek + 6 , 7 ) - 1 ) DAYs
          FROM LATERAL(VALUES DATE('0001-01-01') + (year_ - 1) YEARs + (month_ - 1) MONTHs) m(firstday)
       ) calculated_date
  FROM (VALUES ( 2010 ,  5 , 2 , 5 )
             , ( 2010 ,  7 , 5 , 1 )
             , ( 2010 ,  7 , 7 , 1 )
             , ( 2010 ,  7 , 1 , 1 )
             , ( 2010 ,  7 , 4 , 1 )
             , ( 2010 , 10 , 2 , 2 )
       ) d( year_ , month_ , dayofweek , weekofmonth )
;
------------------------------------------------------------------------------

YEAR_       MONTH_      DAYOFWEEK   WEEKOFMONTH CALCULATED_DATE
----------- ----------- ----------- ----------- ---------------
       2010           5           2           5 2010-05-31     
       2010           7           5           1 2010-07-01     
       2010           7           7           1 2010-07-03     
       2010           7           1           1 2010-07-04     
       2010           7           4           1 2010-07-07     
       2010          10           2           2 2010-10-11     

  6 record(s) selected.
Reply With Quote
  #5 (permalink)  
Old 08-13-10, 10:47
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
thanks, that definitely is quite complex.. will let you know if I come up with a simpler anwser
Reply With Quote
  #6 (permalink)  
Old 08-13-10, 13:26
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
I tried it out and I get this --
db2 "SELECT (SELECT firstday + ( weekofmonth * 7 - MOD( DAYOFWEEK(firstday) - dayofweek + 6 , 7 ) - 1 ) DAYs FROM LATERAL(VALUES DATE('0001-01-01') + (year_ - 1) YEARs + (month_ - 1) MONTHs) m(firstday) ) calculated_date FROM (VALUES ( 2010 , 10 , 2 , 2 ) ) d( year_ , month_ , dayofweek , weekofmonth )"

CALCULATED_DATE
---------------
10/11/2010

How can I get result returned in the format -- 2010-10-11-00.00.00.000000

thanks!
Reply With Quote
  #7 (permalink)  
Old 08-13-10, 13:27
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
interesting that it doesn't return the result in the same format that you get..
Reply With Quote
  #8 (permalink)  
Old 08-13-10, 14:16
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
interesting that it doesn't return the result in the same format that you get..
The display format of date data type depends on locale.

Quote:
How can I get result returned in the format -- 2010-10-11-00.00.00.000000
TIMESTAMP(calculated_date)

Here is an example:
Code:
------------------------------ Commands Entered ------------------------------
WITH
 calculate AS (
SELECT year_ , month_ , dayofweek , weekofmonth
     , (SELECT first_day + ( weekofmonth * 7 - MOD( DAYOFWEEK(first_day) - dayofweek + 6 , 7 ) - 1 ) DAYs
          FROM LATERAL(VALUES DATE('0001-01-01') + (year_ - 1) YEARs + (month_ - 1) MONTHs ) m(first_day)
       ) calculated_date
  FROM (VALUES ( 2010 ,  5 , 2 , 5 )
             , ( 2010 ,  7 , 5 , 1 )
             , ( 2010 ,  7 , 7 , 1 )
             , ( 2010 ,  7 , 1 , 1 )
             , ( 2010 ,  7 , 4 , 1 )
             , ( 2010 , 10 , 2 , 2 )
       ) d( year_ , month_ , dayofweek , weekofmonth )
)
SELECT year_ , month_ , dayofweek , weekofmonth
     , calculated_date             AS date_local
     , CHAR(calculated_date , USA) AS date_usa
     , CHAR(calculated_date , ISO) AS date_iso
     , TIMESTAMP(calculated_date)  AS format_timestamp
  FROM calculate
;
------------------------------------------------------------------------------

YEAR_       MONTH_      DAYOFWEEK   WEEKOFMONTH DATE_LOCAL DATE_USA   DATE_ISO   FORMAT_TIMESTAMP   
----------- ----------- ----------- ----------- ---------- ---------- ---------- -------------------
       2010           5           2           5 2010-05-31 05/31/2010 2010-05-31 2010-05-31-00.00.00
       2010           7           5           1 2010-07-01 07/01/2010 2010-07-01 2010-07-01-00.00.00
       2010           7           7           1 2010-07-03 07/03/2010 2010-07-03 2010-07-03-00.00.00
       2010           7           1           1 2010-07-04 07/04/2010 2010-07-04 2010-07-04-00.00.00
       2010           7           4           1 2010-07-07 07/07/2010 2010-07-07 2010-07-07-00.00.00
       2010          10           2           2 2010-10-11 10/11/2010 2010-10-11 2010-10-11-00.00.00

  6 record(s) selected.
Reply With Quote
  #9 (permalink)  
Old 08-13-10, 17:28
db2user24 db2user24 is offline
Registered User
 
Join Date: Nov 2007
Posts: 248
thanks, i actually used your command like this and it gives me what I want --

SELECT TIMESTAMP(DATE((SELECT firstday + ( nknthdow * 7 - MOD( DAYOFWEEK(firstday) - nkdow + 6 , 7 ) - 1 ) DAYs FROM
LATERAL(VALUES DATE('0001-01-01') + (nkyear - 1) YEARs + (nkmonth - 1) MONTHs) m(firstday) )) , TIME('00.00.00')) FROM SYSIBM.SYSDUMMY1


Another question I have is -- do you know if there's any way to create a SQL based function in DB2 that takes 'Month', 'Day', 'Year' and 'Timezone' as parameters and returns the time with the correct gmt offset?

I don't think that DB2 is very timezone oriented so not sure if I can achieve this.. for example, let's call the function GET_OFFSET.. it will have 4 parameters

GET_OFFSET ( month INTEGER, day INTEGER, year INTEGER, timezone VARCHAR(64))

--> example : month = 7, day = 1, year =2010, timezone = 'US/Central'
--> result returned = 2010-07-01-05.00.00.000000 since the GMT offset is 5 hours

thanks!
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 On
Pingbacks are On
Refbacks are On