字符串中的SQL搜索字符串

问题描述 投票:-8回答:2

我怎么能在字符串中搜索字符串?

例如:

“状态从'打开'变为'关闭'”

新列应返回“已关闭”

非常感谢。

sql
2个回答
0
投票

在字符串中搜索字符串可以使用:

CHARINDEX ( expressionToFind , expressionToSearch [ , start_location ] )

CHARINDEX Microsoft Documentation


0
投票

您没有提供足够的上下文来正确回答您的问题,示例数据等,但这里有一些您可以开始的事情。

SELECT 'Closed' AS [NEW_STATUS], *
FROM [YourTable]
WHERE [STATUS] -- or whatever the name of the column is that contains the status change text
LIKE "%to 'Closed'%"

替代解决方案取决于您的具体情况:

SELECT
CASE WHEN [StatusText] LIKE "%to 'Closed%"
     THEN 'Closed'
     WHEN [StatusText] LIKE "%to 'Open%"
     THEN 'Open'
END AS [CurrentStatus]
FROM [YourTable]
© www.soinside.com 2019 - 2024. All rights reserved.