Teradata - 根据是否以数字开头在两列之间进行选择

时间:2017-12-26 19:52:36

标签: sql case teradata case-when

我的查询类似于:

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 u.colm = t.cold
WHERE cast(s.cola as date) between date '2017-11-06' and date '2017-11-10'
ORDER BY 3

我需要添加一个名为col_new的新列,该列将由u.colmu.coln填充。如果该列以数字开头,则此列将具有u.colm的值。否则它将具有u.coln的值。众所周知,对于表u.coln中的每个条目,u.colmu都以数字开头。

我尝试了以下查询来测试是否可以识别以数字开头的条目:

SELECT CASE WHEN ISNUMERIC(SUBSTRING(LTRIM(colm), 1, 1)) = 1
        THEN 'yes'
        ELSE 'no'
      END AS col_new
FROM table_u

它返回了错误:Syntax error: expected something between '(' and the 'substring' keyword.

请提出解决方案。

修改 确切错误:

[Teradata Database] [3706] Syntax error: expected something between '(' and the 'substring' keyword.

1 个答案:

答案 0 :(得分:1)

而不是isnumeric(),只需进行比较:

SELECT (CASE WHEN LEFT(LTRIM(colm), 1) BETWEEN '0' AND '9' THEN 'yes'
             ELSE 'no'
        END) AS col_new
FROM table_u;

LEFT()是字符串的第一个“n”字符的便捷简写。