6

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.

jrharshath
  • 25,975
  • 33
  • 97
  • 127
  • Title phrasing is very important if you want to attract people to your question. "My query is broken" is a kind of title most people will ignore. However, if your title indicates you have a specific problem people will be more likely to take a closer look. – Welbog Jun 24 '09 at 14:19

3 Answers3

17

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!

jrharshath
  • 25,975
  • 33
  • 97
  • 127
2

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 )

Fred Sobotka
  • 5,252
  • 22
  • 32
1

You should be able to cast the selected id column to match the data type of corr_id

select CORR_ID from TABLE1 where CORR_ID not in (select cast(id as varchar) from TABLE2)

GregA100k
  • 1,385
  • 1
  • 11
  • 16