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 > Compare Date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-28-08, 16:31
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Compare Date

Hello,

I am new to DB2 and this is my first post. I need to see if a date column 'YRGIVING' is equal to the current year 2008. I have this in my query 'and YRGIVING = YEAR(CURRENT DATE)'.

I am getting this error:
SQLCODE = -401, ERROR: THE OPERANDS OF AN ARITHMETIC OR COMPARISON OPERATION ARE NOT COMPARABLE

This will be an automatic process so I can't hardcode '2008' in the query. Can anyone help me please?

Thank you!
Reply With Quote
  #2 (permalink)  
Old 08-28-08, 17:22
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Kevin,
Off the top of the head guess, a data type mismatch. Your yrgiving column is possibly defined as CHAR??

Dave
Reply With Quote
  #3 (permalink)  
Old 08-28-08, 18:02
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
If YRGIVING is defined as a DB2 date column, then it has more than just the year.

Try: 'and YEAR(YRGIVING) = YEAR(CURRENT DATE)'.

If YRGIVING is not a date column, then dav1mo probably has the correct answer.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 08-29-08, 08:52
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Thank you both for your help!!

YRGIVING is defined as CHAR. So it is a data type mismatch.
How can I use YRGIVING to compare the date in this case?

Could I use 'cast' to mask YRGIVING as a date type?

Last edited by KevinYC; 08-29-08 at 09:29.
Reply With Quote
  #5 (permalink)  
Old 08-29-08, 09:28
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
I did a quick search on this forum and came up with this :

YRGIVING is defined as char (04);
FYSTART is defined as date;

SELECT CAST(YEAR(B.FYSTART) AS CHAR(04))
FROM TB945 A,
TBDAT B
WHERE A.ENTITYID = B.ENTITYID
AND A.YRGIVING = B.FYSTART

This is the error I got:

SQLCODE = -180, ERROR: THE DATE, TIME, OR TIMESTAMP VALUE YRGIVING IS INVALID

Any suggestions?
Regards,
Kevin

Last edited by KevinYC; 08-29-08 at 09:34.
Reply With Quote
  #6 (permalink)  
Old 08-29-08, 09:34
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Try:

'and cast (YRGIVING as int) = YEAR(CURRENT DATE)';
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #7 (permalink)  
Old 08-29-08, 09:46
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Marcus,

That worked beautifully !!!
Thanks!

Kevin
Reply With Quote
  #8 (permalink)  
Old 08-29-08, 10:21
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
also, just so you know, you casted what you were selecting as char, but in your where clause you were comparing a char 4 column to a date column, therefore the -180. If you were to put the cast function in the where clause when you comapre the 2 columns it will work as did Marcus' solution with casting the yrgiving as an int. Keep in mind issues with indexing and if you will be able to match columns as these tables grow.

Dave
Reply With Quote
  #9 (permalink)  
Old 08-29-08, 13:56
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Dave,

Thanks for the input. The more I play with this the more it makes sense. You guys are great. Thanks again!

Kevin
Reply With Quote
  #10 (permalink)  
Old 08-30-08, 09:23
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
YRGIVING is defined as char (04);
FYSTART is defined as date;

SELECT CAST(YEAR(B.FYSTART) AS CHAR(04))
FROM TB945 A,
TBDAT B
WHERE A.ENTITYID = B.ENTITYID
AND A.YRGIVING = B.FYSTART

This is the error I got:

SQLCODE = -180, ERROR: THE DATE, TIME, OR TIMESTAMP VALUE YRGIVING IS INVALID
Another ideas(convert or use expression only for one column):
1) AND A.YRGIVING = SUBSTR(CHAR(B.FYSTART), 1, 4)
2) AND B.FYSTART BETWEEN DATE(A.YRGIVING||'-01-01') AND DATE(A.YRGIVING||'-12-31')
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