解析单元格中的文本并将其转换为多列数据

Parsing the text in a cell and converting it into data in multiple columns

Table : test
|---------------------------------------------------------| 
|      descriptiona                                       |
|---------------------------------------------------------|
|#*Summary: data1  #*Steps: data2  #*Result: data3        |        
|---------------------------------------------------------|
|#*Steps: data5  #*Summary: data6  #*Result: data4        |
|---------------------------------------------------------|

我希望数据显示为:

summary   steps   result
data1     data2   data3
data6     data5   data4 

尝试使用:

SELECT  substring(descriptiona, 1, charindex('*Steps', descriptiona)-2) AS Summary,
  substring(descriptiona, charindex('*Steps', descriptiona), (charindex('*Result', descriptiona) - charindex('*Steps', descriptiona)) -2 ) AS Steps,
       substring(descriptiona, charindex('*Result', descriptiona),len(descriptiona)) AS ActualResult
 from test;

但这只适用于第一行。

您可以拆分字符串并在 CROSS APPLY

中执行条件聚合

例子

Declare @YourTable Table ([descriptiona] varchar(50))  Insert Into @YourTable Values 
 ('#*Summary: data1 #*Steps: data2 #*Result: data3')
,('#*Steps: data5 #*Summary: data6 #*Result: data4')

Select B.* 
 From  @YourTable
 Cross Apply ( Select Summary= stuff(max(case when charindex('Summary:',value)>0 then Value end),1,10,'')
                     ,Steps  = stuff(max(case when charindex('Steps:',value)>0 then Value end)  ,1,8,'')
                     ,Result = stuff(max(case when charindex('Result:',value)>0 then Value end) ,1,9,'')
                 From string_split([descriptiona],'#')
             ) B

Returns

Summary Steps   Result
data1   data2   data3
data6   data5   data4

编辑 - 2012 非功能选择

Declare @YourTable Table ([descriptiona] varchar(50))  Insert Into @YourTable Values 
 ('#*Summary: data1 #*Steps: data2 #*Result: data3')
,('#*Steps: data5 #*Summary: data6 #*Result: data4')

Select B.* 
 From  @YourTable
 Cross Apply ( Select Summary= stuff(max(case when charindex('Summary:',value)>0 then Value end),1,10,'')
                     ,Steps  = stuff(max(case when charindex('Steps:',value)>0 then Value end)  ,1,8,'')
                     ,Result = stuff(max(case when charindex('Result:',value)>0 then Value end) ,1,9,'')
                 From (
                        Select seq   = row_number() over (order by 1/0)
                              ,value = ltrim(rtrim(B.i.value('(./text())[1]', 'varchar(max)')))
                        From  (Select x = Cast('<x>' + replace((Select replace([descriptiona],'#','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
                        Cross Apply x.nodes('x') AS B(i)
                      ) B1
             ) B

SQL Fiddle

只是为了好玩,如果你是 运行 SQL Server 2016 或最新版本,这是一个可能的解决方案(不是最漂亮的,不可否认):

SELECT
    JSON_VALUE(json_string, '$.Summary') as Summary,
    JSON_VALUE(json_string, '$.Steps') as Steps,
    JSON_VALUE(json_string, '$.Result') as Result
FROM (
        SELECT 
      '{"'+REPLACE(
        REPLACE(
          REPLACE(SUBSTRING(description, 3, LEN(description)), ' ', ''),
          ':', '":"'),
        '#*', '","') 
        + '"}' AS json_string
FROM test) A

我们的想法是从您的 descriptions 中获取一个 JSON 字符串,然后这样解析它并提取相关字段,例如

#*Summary: data1  #*Steps: data2  #*Result: data3

变成

{"Summary":"data1","Steps":"data2","Result":"data3"}

当然有一些注意事项,主要取决于您 table 中的实际值。