Convert char to int TeraData Sql

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

i'm trying to convert a column from char (8) to integer in order to make a referential integrity with an integer. IT didn't work and I test a select in order to check the cast. Utente_cd is a char(8) column

SEL  CAST(UTENTE_CD AS INTEGER)  FROM TABLEA 

Teradata produces this error :

SELECT Failed. 2621: Bad character in format or data of TABLEA.

Sometime the char column contains also an alphanumeric code that I should discard.

Thank you

回答1:

In TD15.10 you can do TRYCAST(UTENTE_CD AS INTEGER) which will return a NULL instead of failing.

Before there are multiple ways:

-- TO_NUMBER returns NULL when failing CAST(TO_NUMBER(UTENTE_CD) AS INTEGER)  -- check if there are only digits CASE WHEN UTENTE_CD  = ''                     -- all spaces        THEN NULL      WHEN LTRIM(UTENTE_CD, '0123456789') = '' -- only digits        THEN CAST(UTENTE_CD AS INTEGER)      ELSE NULL END 


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