Teradata SUBSTRING指数超出界限

时间:2017-07-14 07:43:08

标签: sql teradata

此查询有效:

SELECT
TOP 100     SUBSTRING(column_name FROM 6 FOR CHARACTER_LENGTH(column_name) - 5) AS X
FROM        db_name.table_name

但是以下查询(添加了WHERE子句)不会执行。

SELECT
TOP 100     SUBSTRING(column_name FROM 6 FOR CHARACTER_LENGTH(column_name) - 5) AS X
FROM        db_name.table_name
WHERE       NOT EXISTS
            (
            SELECT    1
            FROM      db_name2.lookup_name H
            WHERE     H.SRC_NUM1 = X 
                      AND H.SRC_TYPE = 11
            )

上面的查询抛出

  

SELECT失败。 2663:SUBSTR:字符串下标超出table_name.column_name

中的范围

但是,以下一个有效(原始SELECT是嵌套的)

SELECT      *
FROM        (
            SELECT
            TOP 100   SUBSTRING(column_name FROM 6 FOR CHARACTER_LENGTH(column_name) - 5) AS X
            FROM      db_name.table_name
            ) A
WHERE       NOT EXISTS
            (
            SELECT    1
            FROM      db_name2.lookup_name H
            WHERE     H.SRC_NUM1 = X 
                      AND H.SRC_TYPE = 11
            )

为什么会这样?我正在使用SQL助手来执行查询,但我怀疑它是否相关。

1 个答案:

答案 0 :(得分:0)

尝试更改(当column_name的长度小于6时可能会导致错误):

single  1
single  1
single  1
single  0
five    1
five    4
five    5
five    4
five    1
five    0