Datatype conversion in IBM DB2: BIGINT to VARCHAR

[亡魂溺海] 提交于 2019-12-08 21:53:08

问题


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.


回答1:


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!




回答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 )




回答3:


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)



来源:https://stackoverflow.com/questions/1038670/datatype-conversion-in-ibm-db2-bigint-to-varchar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!