查找子串 - SQL

时间:2018-01-23 16:09:17

标签: sql substring querying

我需要找到一个文本字段中实际上是部分xml的子字符串。我尝试将其转换为xml,然后使用.value方法,但无济于事。

我正在寻找的元素(子字符串)是一个如下所示的方法名称:

AssemblyQualifiedName="IPMGlobal.CRM2011.IPM.CustomWorkflowActivities.ProcessChildRecords,

其中“ProcessChildRecords”末尾的方法可能是另一个名称,例如“SendEmail”。我知道我可以使用“ CustomWorkflowActivities。”和,(逗号)来查找子字符串(方法名称),但不知道如何完成它。此外,**"CustomWorkflowActvities.<method>"**

列出的实例可能更多

一些澄清:

以下是我的原始查询。它返回每行中第一次出现但没有额外的行。例如,我可能在字符串'... IPM.CustomWorkflowActivities.ProcessChildRecords ...'和 的 '... IPM.CustomWorkflowActivities.GetworkflowContext ...'

当前查询仅返回批准时间流程, ipm_mytimesheetbatch, ProcessChildRecords

SELECT WF.name WFName, ( SELECT TOP 1 Name FROM entity E WHERE WF.primaryentity = E.ObjectTypeCode ) Entity, Convert(xml, xaml) Xaml, SUBSTRING(xaml, Charindex('CustomWorkflowActivities.', xaml) + Len('CustomWorkflowActivities.'), Charindex(', IPMGlobal.CRM2011.IPM.CustomWorkflowActivities, Version=1.0.0.0', xaml) - Charindex('CustomWorkflowActivities.', xaml) - Len('CustomWorkflowActivities.'))
FROM FilteredWorkflow WF

1 = 1       AND xaml LIKE'%customworkflowactivities%'       AND statecodename ='已激活'       AND typename ='定义' 按名称命令

1 个答案:

答案 0 :(得分:0)

如果您使用的是Oracle,则可以使用REGEXP功能:

WITH cte(t) as (
  SELECT 'AssemblyQualifiedName="IPMGlobal.CRM2011.IPM.CustomWorkflowActivities.ProcessChildRecords,' FROM dual
)
SELECT t,
     regexp_replace(t, '.*CustomWorkflowActivities.(.+)\,.*', '\1') AS r
FROM cte;

<强> DBFiddle Demo

SQL Server:

WITH cte(t) as (
  SELECT 'AssemblyQualifiedName="IPMGlobal.CRM2011.IPM.CustomWorkflowActivities.ProcessChildRecords,asfdsa'
)
SELECT t,SUBSTRING(t, s, CHARINDEX(',', t, s)-s)
FROM (SELECT t, PATINDEX( '%CustomWorkflowActivities.%', t) + LEN('CustomWorkflowActivities.') AS s
      FROM cte 
) sub;

<强> DBFiddle Demo 2