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

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

我有一个看起来类似于的查询:

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.colnu.colm以数字开头,表u中的每个条目。

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

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.
sql case teradata case-when
1个回答
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”字符的便捷简写。

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