Split String Column values

僤鯓⒐⒋嵵緔 提交于 2021-02-05 07:56:25

问题


acctcode         primekey          groupby    <--- columns

 WDS                1              'NULL'     <--- values (varchar)       
 FDS                2              'NULL'
 IRN                3              'NULL'
 SUM                4              1,2,3
 STL                5              'NULL'
 WTR                6              'NULL'
 SUM2               7               5,6 

I want to split string groupby column where values are NOT EQUAL to 'NULL' and save it to another table which will look like this:

acctcode          primekey         groupby
 SUM                 4                1
 SUM                 4                2
 SUM                 4                3
 SUM2                7                5
 SUM2                7                6

here is my split string code:

    ALTER FUNCTION [dbo].[SplitStrings]
(
   @List       NVARCHAR(MAX),
   @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
   RETURN 
   (  
      SELECT primekey = y.i.value('(./text())[1]', 'nvarchar(4000)')
      FROM 
      ( 
        SELECT x = CONVERT(XML, '<i>' 
          + REPLACE(@List, @Delimiter, '</i><i>') 
          + '</i>').query('.')
      ) AS a CROSS APPLY x.nodes('i') AS y(i)
   );

here is the code I use to call the function:

SELECT acctcode,(SELECT * FROM SplitStrings(groupby,','))as prime 
INTO Chadtblsum
FROM Chadothercharges WHERE acctcode = acctcode

The code above gives me this error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

回答1:


Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.

The above error happens since your subquery in the SELECT returns more than one row. Try executing this:

SELECT * FROM SplitStrings('1,2,3',',') x

You'll see that it'll return 3 rows, one for each item.

In order to fix this, you have to use CROSS APPLY:

SQL Fiddle

SELECT 
    c.acctcode,
    x.primekey AS prime
INTO Chadtblsum
FROM Chadothercharges c
CROSS APPLY SplitStrings(c.groupby,',') x
WHERE groupby <> 'NULL'



回答2:


declare  @Table1 TABLE 
    ([acctcode] varchar(4), [primekey] int, [groupby] varchar(8))
;

INSERT INTO @Table1
    ([acctcode], [primekey], [groupby])
VALUES
    ('WDS', 1, NULL),
    ('FDS', 2, NULL),
    ('IRN', 3, NULL),
    ('SUM', 4, '1,2,3'),
    ('STL', 5, NULL),
    ('WTR', 6, NULL),
    ('SUM2', 7, '5,6')
;

select   [acctcode], [primekey],
         Split.a.value('.', 'VARCHAR(100)') AS SubColour  
     FROM  (SELECT [acctcode], [primekey],
             CAST ('<M>' + REPLACE([groupby], ',', '</M><M>') + '</M>' AS XML) AS String  
         FROM  @Table1) AS A CROSS APPLY String.nodes ('/M') AS Split(a) 


来源:https://stackoverflow.com/questions/31822442/split-string-column-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!