Substr:字符串越界错误

时间:2014-09-24 06:05:39

标签: teradata

我正在为下面的substr获取字符串超出边界的错误。我怎么能克服这个?

 SEL DRAWING_NUMBER
    ,CASE 
      WHEN SUBSTR(DRAWING_NUMBER,1,1) ='Y' THEN  SUBSTR(DRAWING_NUMBER ,1,(INDEX(DRAWING_NUMBER , '.')-1))
    ELSE  DRAWING_NUMBER
    END AS DERIVED_DRAWING
     FROM  GEEDW_D_PLP_BULK_V.CDR_DOCUMENTUM_TRSMTL

2 个答案:

答案 0 :(得分:1)

DRAWING_NUMBER的一些以'Y'开头的值,很可能不包含'。' characher。 在这种情况下,INDEX(DRAWING_NUMBER, '.')将返回0.在这种情况下,您的SUBSTR(DRAWING_NUMBER ,1,(INDEX(DRAWING_NUMBER , '.')-1)将评估为SUBSTR(DRAWING_NUMBER, 1, -1),这超出了SUBSTR功能的定义范围。

你可能想要完成的是:

SEL 
  DRAWING_NUMBER
  ,CASE 
    WHEN (SUBSTR(DRAWING_NUMBER, 1, 1) ='Y') AND (INDEX(DRAWING_NUMBER , '.') > 0)
      THEN  SUBSTR(DRAWING_NUMBER, 1, (INDEX(DRAWING_NUMBER , '.') - 1))
    ELSE DRAWING_NUMBER
  END AS DERIVED_DRAWING
FROM GEEDW_D_PLP_BULK_V.CDR_DOCUMENTUM_TRSMTL;

答案 1 :(得分:0)

您可以使用LIKE重写它:

CASE WHEN DRAWING_NUMBER LIKE 'Y%.%' 
     THEN SUBSTR(DRAWING_NUMBER ,1,(INDEX(DRAWING_NUMBER , '.')-1)) 
     ELSE DRAWING_NUMBER
END

您也可以切换到标准SQL:

CASE WHEN DRAWING_NUMBER LIKE 'Y%.%' 
     THEN SUBSTRING(DRAWING_NUMBER FROM 1 FOR POSITION('.' IN DRAWING_NUMBER) -1) 
     ELSE DRAWING_NUMBER
END