简体   繁体   English

拆分字符串Teradata SQL

[英]Split String Teradata SQL

I'm looking to split a string in Teradata. 我正在寻找在Teradata中拆分字符串的方法。

The table might look something like this. 该表可能看起来像这样。

column1
hello:goodbye:afternoon

I'm trying to use SUBSTRING and INSTR to extract specific words. 我正在尝试使用SUBSTRING和INSTR提取特定的单词。 So, say I want to select "goodbye". 所以,说我想选择“再见”。 I'm trying the following query. 我正在尝试以下查询。

SELECT SUBSTRING(a.column1 from index(a.column1,':')+1 for INSTR(a.column1,':',0,2))
FROM db.table as a

I get the following error. 我收到以下错误。

SELECT Failed. [3707] Syntax error, expected something like ')' between the word 'INSTR' and '('

I'm not sure why I'm getting that error. 我不确定为什么会收到该错误。 It lets me use INDEX to deduce a number in place of INSTR, so I'm not sure why it is acting this way when I use INSTR. 它使我可以使用INDEX来代替INSTR来推导一个数字,因此我不确定为什么当我使用INSTR时它为什么以这种方式起作用。

If this was TD14 you wouldn't need INSTR , there's a STRTOK function :-) 如果这是TD14,则不需要INSTR ,则有一个STRTOK函数:-)

STRTOK(column1,':',2),

For earlier releases it's 对于早期版本,它是

CASE 
   WHEN column1 LIKE '%:%:%'
   THEN SUBSTRING(column1 FROM POSITION(':' IN column1) + 1 FOR POSITION(':' IN 
        SUBSTRING(column1 FROM POSITION(':' IN column1) + 1)) - 1)
   WHEN column1 LIKE '%:%'
   THEN SUBSTRING(column1 FROM POSITION(':' IN column1) + 1)
END

The CASE LIKE ist just to prevent an " string subscript out of bound " error when there no colon. CASE LIKE ist只是为了防止在没有冒号时出现“ 字符串下标越界 ”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM