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 CHAR columns using CAST

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-01-08, 10:12
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Compare CHAR columns using CAST

SELECT CAST(B.YEARGIVE AS INTEGER), CAST(C.FISCYEAR AS INTEGER)
FROM
ADVTB950 B,
ADVTBDAT C
WHERE B.YEARGIVE >= C.FISCYEAR - 1;

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


I am trying to compare two CHAR columns. Both columns contain a numeric year value. I tried using CAST but got an error. Any suggestions?

Thanks for any input.
Reply With Quote
  #2 (permalink)  
Old 10-01-08, 10:27
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I'd say you're using CAST() in the wrong place.
Reply With Quote
  #3 (permalink)  
Old 10-01-08, 10:44
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Can I subtract 1 from a CHAR column without using CAST? Is there a way?
Reply With Quote
  #4 (permalink)  
Old 10-01-08, 10:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Certainly, you could use INT() if for some reason you hold a grudge against CAST()...
Reply With Quote
  #5 (permalink)  
Old 10-01-08, 10:58
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Quote:
Originally Posted by n_i
Certainly, you could use INT() if for some reason you hold a grudge against CAST()...
I am willing to use anything. Just want to get this to work..
How would I use INT() here?
Thanks.
Reply With Quote
  #6 (permalink)  
Old 10-01-08, 18:17
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
Hey Kevin,
Is it that your columns are really defined as CHAR? If so, then you are casting them in the wrong place. you need to cast them in the where clause or as N_I stated jus tuse int. Like:
SELECT CAST(B.YEARGIVE AS INTEGER), CAST(C.FISCYEAR AS INTEGER)
FROM
ADVTB950 B,
ADVTBDAT C
WHERE int(B.YEARGIVE) >= int(C.FISCYEAR) - 1;

Dave
Reply With Quote
  #7 (permalink)  
Old 10-02-08, 15:51
KevinYC KevinYC is offline
Registered User
 
Join Date: Aug 2008
Posts: 42
Thank you, Dave and n_i !!
It worked this way with or without CAST.

SELECT B.YEARGIVE, C.FISCYEAR
FROM
ADVTB950 B,
ADVTBDAT C
WHERE INT(B.YEARGIVE) >= INT(C.FISCYEAR) - 1;
Reply With Quote
  #8 (permalink)  
Old 10-04-08, 06:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Whether you call it CAST or "conversion to integer with INT()" doesn't make any difference in the semantics. Both do the same thing.

So what's your problem with a CAST? I didn't understand that.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 10-04-08, 14:14
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Actually, I would say that the table design was not well chosen: if years need to be numerically manipulated, they should be stored as SMALLINT instead of as CHAR(4).

Views could "mask" this as an implicit conversion (until the tables have been redesigned).
So essentially your query could become:
Code:
WITH tb950 (yeargive) AS
( SELECT CAST(yeargive AS smallint) FROM advtb950 ) ,
tbdat (fiscyear) AS
( SELECT CAST(fiscyear AS smallint) FROM advtbdat )
SELECT B.yeargive, C.fiscyear
FROM tb950 B INNER JOIN tbdat C ON B.yeargive >= C.fiscyear - 1
and the "WITH" (view) would disappear when the tables have been redesigned to used SMALLINT instead of CHAR(4) for the year columns.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
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