从SQL Server中的字符串中获取分号前的单词

时间:2017-08-01 15:01:16

标签: sql-server tsql

我有以下字符串:

#load "../Shared/SomeSharedFile.csx"

需要在Consolidation CompletedThe Scenario is LDP; the Year is 2018; the Start Period is July; the End Period is June; the Entity is TOT_NEWS.前面的单词和semicolon (;)

之前的最后一个单词
dot (.)

我可以通过以下选择获得第一个Result: ----- ---- ---- ---- ---- LDP 2018 July June TOT_NEWS

LDP

但它对字符串的其余部分不起作用。

3 个答案:

答案 0 :(得分:2)

这是一种使用字符串拆分器的方法......

declare @value varchar(max) = 'Consolidation CompletedThe Scenario is LDP; the Year is 2018; the Start Period is July; the End Period is June; the Entity is TOT_NEWS.'

;with cte as(
select
* from
dbo.DelimitedSplit8K(@value,';'))

select
    replace(right(Item,charindex(' ',reverse(Item),1)),'.','')
from cte

功能

CREATE FUNCTION [dbo].[DelimitedSplit8K] (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!

RETURNS TABLE WITH SCHEMABINDING AS
RETURN

/* "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
enough to cover VARCHAR(8000)*/

  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l
;
GO

Creator of the Function

答案 1 :(得分:2)

如果您对TVF(表格值函数)持开放态度。

此方法使用修改的分割/解析功能。我使用了两个非类似的分隔符,而不是一个分隔符。在这种情况下,' '';'

示例

Declare @YourTable table (id int,strDescription varchar(max))
Insert Into @YourTable values
(1,'Consolidation CompletedThe Scenario is LDP; the Year is 2018; the Start Period is July; the End Period is June; the Entity is TOT_NEWS.')

Select A.ID
      ,B.*
 From  @YourTable A
 Cross Apply (
              Select Pos1 = max(case when RetSeq=1 then RetVal end)
                    ,Pos2 = max(case when RetSeq=2 then RetVal end)
                    ,Pos3 = max(case when RetSeq=3 then RetVal end)
                    ,Pos4 = max(case when RetSeq=4 then RetVal end)
                    ,Pos5 = max(case when RetSeq=5 then RetVal end)
               From [dbo].[udf-Str-Extract](A.strDescription+';',' ',';')
             ) B

<强>返回

ID  Pos1    Pos2    Pos3    Pos4    Pos5
1   LDP     2018    July    June    TOT_NEWS

感兴趣的UDF

CREATE FUNCTION [dbo].[udf-Str-Extract] (@String varchar(max),@Delimiter1 varchar(100),@Delimiter2 varchar(100))
Returns Table 
As
Return (  

with   cte1(N)   As (Select 1 From (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) N(N)),
       cte2(N)   As (Select Top (IsNull(DataLength(@String),0)) Row_Number() over (Order By (Select NULL)) From (Select N=1 From cte1 N1,cte1 N2,cte1 N3,cte1 N4,cte1 N5,cte1 N6) A ),
       cte3(N)   As (Select 1 Union All Select t.N+DataLength(@Delimiter1) From cte2 t Where Substring(@String,t.N,DataLength(@Delimiter1)) = @Delimiter1),
       cte4(N,L) As (Select S.N,IsNull(NullIf(CharIndex(@Delimiter1,@String,s.N),0)-S.N,8000) From cte3 S)

Select RetSeq = Row_Number() over (Order By N)
      ,RetPos = N
      ,RetVal = left(RetVal,charindex(@Delimiter2,RetVal)-1) 
 From  (
        Select *,RetVal = Substring(@String, N, L) 
         From  cte4
       ) A
 Where charindex(@Delimiter2,RetVal)>1

)
/*
Max Length of String 1MM characters

Declare @String varchar(max) = 'Dear [[FirstName]] [[LastName]], ...'
Select * From [dbo].[udf-Str-Extract] (@String,'[[',']]')
*/

答案 2 :(得分:1)

你走了:

string                                                                                                                                  |  Result_1 | Result_2 | Result_3 | Result_4 | Result_5
----------------------------------------------------------------------------------------------------------------------------------------|-----------|----------|----------|----------|---------
Consolidation CompletedThe Scenario is LDP; the Year is 2018; the Start Period is July; the End Period is June; the Entity is TOT_NEWS. |  LDP      | 2018     | July     | June     | TOT_NEWS

输出:

{{1}}