如果你有两个不同的单词,你怎么得到一个单词之后的子字符串?我希望在'是'和'不是'之后的子字符串。假设我有如下列。如何在单个查询中的'is'和'is not'之后抓取子字符串?我创建了另一个列并具有空值。我只想要一列而没有空值
"bob is very cool"
"Cris isn't cool"
SELECT SUBSTR(String, INDEX_OF(String,'isnt'),strlen(string)) as column2,
SUBSTR(String,INDEX_OF(String,'is'),STRLEN(String)) as column1 from
'[LOGFILEPATH]' where column2 <> null or column1 <>null
预期结果
"very cool"
"cool"
答案 0 :(得分:0)
使用INSTR
查找关键字的位置。
select case when instr(test_string,'isn''t') > 0
then substr(test_string,
instr(test_string,'isn''t')+5,
length(test_string))
when instr(test_string,' is ') > 0
then substr(test_string,
instr(test_string,' is ')+4,
length(test_string))
else null
end as stringAftKeyWord
from table
答案 1 :(得分:0)
如果你知道你需要什么词,你可以做一个Case语句。
SELECT 'bob is very cool' AS whoIsCool INTO #temp
INSERT INTO #temp VALUES ('Cris isn''t cool')
SELECT * FROM #temp
SELECT
CASE
WHEN whoIsCool like '%is %' THEN
SUBSTRING(whoIsCool, CHARINDEX('is', whoIsCool) + LEN('is'), CHARINDEX('is', whoIsCool) + LEN(whoIsCool))
WHEN whoIsCool like '%isn''t%' THEN
SUBSTRING(whoIsCool, CHARINDEX('isn''t', whoIsCool) + LEN('isn''t'), CHARINDEX('isn''t', whoIsCool) + LEN(whoIsCool))
END as newCool
FROM #temp
DROP TABLE #temp
另外,你应该使用CONTAINS()而不是通配符(就像我在这里)。