| |
|
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.
|
 |

08-28-08, 16:31
|
|
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!
|
|

08-28-08, 17:22
|
|
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
|
|

08-28-08, 18:02
|
|
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
|
|

08-29-08, 08:52
|
|
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.
|

08-29-08, 09:28
|
|
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.
|

08-29-08, 09:34
|
|
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
|
|

08-29-08, 09:46
|
|
Registered User
|
|
Join Date: Aug 2008
Posts: 42
|
|
Marcus,
That worked beautifully !!!
Thanks!
Kevin
|
|

08-29-08, 10:21
|
|
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
|
|

08-29-08, 13:56
|
|
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 
|
|

08-30-08, 09:23
|
|
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')
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|