Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatype conversion in IBM DB2: BIGINT to VARCHAR

Tags:

sql

db2

udb

I'm writing a query to do some stuff. But its not working the way I want it to:

select CORR_ID from TABLE1
where CORR_ID not in (select id from TABLE2)

The problem is, TABLE2.id is a long, while TABLE1.CORR_ID is a string.

So how can I make it work?

PS: I'm using IBM UDB.

like image 749
jrharshath Avatar asked Jun 24 '09 14:06

jrharshath


People also ask

What is cast function in DB2?

DB2 CAST is a function available in DB2 that is used for explicit conversion of the data type of a particular value to another datatype.

What Sqlcode 420?

-420 THE VALUE OF A STRING ARGUMENT WAS NOT ACCEPTABLE TO THE function-name FUNCTION.

How is varchar stored in DB2?

DB2 varchar ordinarily holds 1 byte for every character and 2 additional bytes for the length data. It is prescribed to utilize varchar as the information type when segments have variable length and the real information is path not exactly the given limit.

What is Bigint in DB2?

A big integer is a binary integer with a precision of 63 bits. The range of big integers is -9223372036854775808 to +9223372036854775807.


2 Answers

Okay, I found a method:

select CORR_ID from TABLE1 where CORR_ID not in 
(select CAST( CAST(id AS CHAR(50)) AS VARCHAR(50) ) from TABLE2)

This is pretty intriguing: You can't cast a BIGINT to VARCHAR, but:

  • you can cast a BIGINT to CHAR
  • and you can cast a CHAR TO VARCHAR

this is ridiculous!

like image 187
jrharshath Avatar answered Sep 23 '22 11:09

jrharshath


DB2 allows a VARCHAR and CHAR column to be compared without additional casting, so all you really need to do is cast the number.

SELECT corr_id FROM table1 WHERE corr_id NOT IN (SELECT CHAR( id ) FROM table2 )

like image 22
Fred Sobotka Avatar answered Sep 24 '22 11:09

Fred Sobotka