用于在多值字段中提取以逗号分隔的值的SQL代码

时间:2017-09-15 15:43:53

标签: sql sql-server tsql split

enter image description here

如何使用SQL Server执行此操作?

5 个答案:

答案 0 :(得分:5)

这是许多受欢迎的分离器之一。

<强> FLATTEN docs

declare @table table (accountnum int, [services] varchar(1000), PIN int)
insert into @table
values
(30200,'ASCF008,ASFTCTAF',111111),
(30200,'AFTCTAF',222222),
(30200,'AFTCTAF,ASCF004',555555)

Select
    accountnum
    ,[services] = Item
    ,PIN
from
    @table
cross apply DelimitedSplit8K([services],',')

<强> ONLINE DEMO

/****** Object:  UserDefinedFunction [dbo].[DelimitedSplit8K]    Script Date: 09/15/2017 10:51:16 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

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

答案 1 :(得分:1)

在SQL Server 2016中有一个STRING_SPLIT()函数。

参考此处:https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql

至于早期版本,这里有很好的答案: Turning a Comma Separated string into individual rows

答案 2 :(得分:0)

尝试这个逻辑,它将逗号分开。

SELECT Accountnum,
   LTRIM(RTRIM(m.n.value('.[1]','varchar(8000)'))) AS [Services],
   PTN
FROM
(
  SELECT 
    CAST('<XMLRoot><RowData>' + REPLACE([Services],',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x
  FROM
  (
    SELECT   Accountnum,
      [Services],
      PTN  
    FROM dbo.TESTTABLE
  ) AS XMLData
) AS Result
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)

答案 3 :(得分:0)

你的桌面设计错了。您需要一个额外的服务表和一个帐户服务表:

tblAccount
AccountID
AccountNum
ServiceID
PTN

tblServices
ServiceID
Service

tblAccountService
AccountID
ServiceID

您的设计违反First Normal Form

答案 4 :(得分:0)

我会用这样的东西。

逗号左边

SELECT LEFT(services,CHARINDEX(',',services)-1) FROM table

逗号权

SELECT Right(services,CHARINDEX(',',REVERSE(services))-1) FROM table