本文介绍了将char转换为int TeraData SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将char(8)的列转换为整数,以使整数具有参照完整性。 IT无效,我测试了一个选区以检查演员表。 Utente_cd是一个char(8)列

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产生此错误:

Teradata produces this error:

有时,char列还包含我应该丢弃的字母数字代码。

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

推荐答案

在TD15.10中,您可以执行 TRYCAST(UTENTE_CD AS INTEGER),它将返回NULL而不是失败。

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

在有多种方法之前:

-- 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

这篇关于将char转换为int TeraData SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:41