gpt4 book ai didi

sql - oracle sql - 选择具有多个 "case when"的语句并检查包含文本

转载 作者:行者123 更新时间:2023-12-04 16:33:27 31 4
gpt4 key购买 nike

我寻找一个工作示例,我可以在其中使用 mutliple when case 语句和 check 来验证是否包含特定文本:
例如

SELECT 
ID,
NAME,
(SELECT
(Case when Contains(Descr,"Test") Then "contains Test"
when Contains(Descr, "Other") Then "contains Other"
Else "No Match" End) From DESCRIPTION
where item_id = id
) as "Match"
From Item

最佳答案

在 Oracle 中,字符串文字需要用单引号括起来。

要查找子字符串匹配,您可以使用 LIKE :

SELECT  ID,
NAME,
CASE WHEN Descr LIKE '%Test%' THEN 'Contains Test'
WHEN Descr LIKE '%Other%' THEN 'Contains Other'
ELSE 'No Match'
END AS Match
FROM Item i
LEFT OUTER JOIN
Description d
ON i.id = d.item_id

INSTR() :
SELECT  ID,
NAME,
CASE WHEN INSTR( Descr, 'Test' ) > 0 THEN 'Contains Test'
WHEN INSTR( Descr, 'Other' ) > 0 THEN 'Contains Other'
ELSE 'No Match'
END AS Match
FROM Item i
LEFT OUTER JOIN
Description d
ON i.id = d.item_id

REGEXP_LIKE() :
SELECT  ID,
NAME,
CASE WHEN REGEXP_LIKE( Descr, 'Test' ) THEN 'Contains Test'
WHEN REGEXP_LIKE( Descr, 'Other' ) THEN 'Contains Other'
ELSE 'No Match'
END AS Match
FROM Item i
LEFT OUTER JOIN
Description d
ON i.id = d.item_id

关于sql - oracle sql - 选择具有多个 "case when"的语句并检查包含文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301031/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com