Teradata - 将Varchar与十进制进行比较

问题描述 投票:0回答:1

我对TeradataSQL一般都很新。我需要通过组合来自三个表的数据来创建表。我能够成功加入其中两个。我无法正确编写第三个表的连接条件。这是代码:

select s.cola, s.colb, 
t.colc, t.cold,
u.cole, u.colf, u.colg, u.colh, u.coli, u.colj, u.colk, u.coll
from table1 s 
inner join table2 t
on s.colb = t.colc
inner join table3 u
on t.cold = cast(u.colm as decimal)
order by 3
where substr(cast(s.cola as varchar(10)),6,2) = 11 and substr(cast(s.cola as varchar(10)),1,4) = 2017 and substr(cast(s.cola as varchar(10)),9,2) between 06 and 10

我得到的错误是:

[Teradata Database] [2620] The format or data contains a bad character.

我认为问题在于:on t.cold = cast(u.colm as decimal)u.colmVARCHAR(50)类型,而t.coldDECIMAL(10, 0)类型。我相信我已经正确地投入了它。请帮忙。谢谢。

sql inner-join teradata
1个回答
3
投票

u.colm有一些不好的数据。

根据您的Teradata版本,您可以使用它进行检查

WHERE u.colm > '' AND TRYCAST(u.colm as decimal(10,0)) ISNULL

要么

WHERE u.colm > '' AND TO_NUMBER(u.colm) IS NULL

您也可以在连接条件中使用它们,例如

on t.cold = trycast(u.colm as decimal(10,0))

不要忘记添加小数的精度,因为它默认为(5,0)

你的WHERE_condition很奇怪,s.cola的数据类型是什么?似乎它是一个带有日期yyyy-mm-dd的字符串。尝试

WHERE trycast(s.cola as date) between date '2017-11-06' and date '2017-11-10' 

最后,ORDER BY应该放在WHERE之后。

© www.soinside.com 2019 - 2024. All rights reserved.