我的查询类似于:
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.
答案 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”字符的便捷简写。