我有一个看起来类似于的查询:
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.colm
或u.coln
填充。如果该列以数字开头,则此列将具有来自u.colm
的值。否则它将具有来自u.coln
的值。众所周知,u.coln
或u.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.
而不是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”字符的便捷简写。