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 > Date where clause assistance needed

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-08-10, 10:38
maxwellhouse maxwellhouse is offline
Registered User
 
Join Date: Oct 2010
Posts: 8
Date where clause assistance needed

I should start off by saying that I am a SQL Server guy, DB2 format is giving me a run for my $$.

I need to retreive records with the following date condition (suedo code below):

SELECT * FROM tablename WHERE
(issue_ts >= 1/1/current year AND< 9/1/current year)
AND
(
(expires_ts is null AND revoked_ts is null AND disabled_ts is null)
OR
(expires_ts > issue_ts + 1 day AND revoked_ts > issue_ts + 1 day AND disabled_ts > issue_ts + 1 day)
)

Any help would be much appreciated!
Reply With Quote
  #2 (permalink)  
Old 12-08-10, 11:06
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
as a quick outline, follow these steps...

first, take DAYOFYEAR(CURRENT DATE) and subtract 1 from it, then subtract that number of days from the current date
Code:
CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY
this gives you january 1st of the current year

then just add 9 months to that for september 1st

and there you have your date range boundaries

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-08-10, 11:16
maxwellhouse maxwellhouse is offline
Registered User
 
Join Date: Oct 2010
Posts: 8
Good start!

Perfect, this gets me started.....

select
CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY AS JAN,
CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY + 8 MONTHS AS SEPT
FROM sysibm.sysdummy1
Reply With Quote
  #4 (permalink)  
Old 12-08-10, 11:21
maxwellhouse maxwellhouse is offline
Registered User
 
Join Date: Oct 2010
Posts: 8
Error when using in Where clause

Why does

where DATE(SUB2.ISSUED_ON_TS) >= CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY
AND DATE(SUB2.ISSUED_ON_TS) < CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) + 8 MONTHS

give me the following error?

"SQLCODE = -171, ERROR: THE DATA TYPE, LENGTH, OR VALUE OF ARGUMENT 2 OF - IS INVALID"


Yet,

select
CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY AS JAN,
CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY + 8 MONTHS AS SEPT
FROM sysibm.sysdummy1

works fine....?
Reply With Quote
  #5 (permalink)  
Old 12-08-10, 11:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
quite right, it's 8 months from jan 1 to sept 1

are you sure you can say + 8 MONTHS?

shouldn't it be + 8 MONTH?

in any case, i think you have to repeat the DAY...
Code:
AND DATE(SUB2.ISSUED_ON_TS) 
  < CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY 
                                        + 8 MONTHS
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-08-10, 11:37
maxwellhouse maxwellhouse is offline
Registered User
 
Join Date: Oct 2010
Posts: 8
active Elmeafty

Quote:
Originally Posted by r937 View Post
quite right, it's 8 months from jan 1 to sept 1

are you sure you can say + 8 MONTHS?

shouldn't it be + 8 MONTH?

in any case, i think you have to repeat the DAY...
Code:
AND DATE(SUB2.ISSUED_ON_TS) 
  < CURRENT DATE - ( DAYOFYEAR(CURRENT DATE) - 1 ) DAY 
                                        + 8 MONTHS

Damn typos...., adding DAY fixed the error. As for MONTH vs MONTHS... I have no idea, both produce the same results. I will use MONTH unless someone says otherwise. Thanks very much for your help!
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