How to convert comma separated NVARCHAR to table records in SQL Server 2005?

前端 未结 7 1208
走了就别回头了
走了就别回头了 2020-11-27 06:18

I have a list of ids separated by comma like:

 1,17,25,44,46,67,88

I want to convert them to a table records ( into a temporary table ) lik

相关标签:
7条回答
  • 2020-11-27 06:24

    Here's a trick that doesn't need a function or XML.

    Basically the string gets transformed into a single insert statement for a temporary table.

    The temp table can then be used for further processing.

    IF OBJECT_ID('tempdb..#tmpNum') IS NOT NULL
          DROP TABLE #tmpNum;
    
    CREATE TABLE #tmpNum (num int);
    
    DECLARE @TEXT varchar(max) = '1,17,25,44,46,67,88';
    
    DECLARE @InsertStatement varchar(max);
    SET  @InsertStatement = 'insert into #tmpNum (num) values ('+REPLACE(@TEXT,',','),(')+');';
    EXEC (@InsertStatement);
    
    -- use the temp table 
    SELECT * 
    FROM YourTable t
    WHERE t.id IN (SELECT DISTINCT num FROM #tmpNum);
    

    This method is usable for up to 1000 values.
    Because 1000 is the max limit of a row value expression.

    Also, as Stuart Ainsworth pointed out.
    Since this method uses Dynamic Sql, be wary of code injection and don't use it for strings based on user input.

    Side-note

    Starting from MS Sql Server 2016, one could simply use the STRING_SPLIT function.

    DECLARE @TEXT varchar(max);
    SET @TEXT = '1,17,25,44,46,67,88';
    
    SELECT t.* 
    FROM YourTable t
    JOIN (SELECT DISTINCT CAST(value AS INT) num FROM STRING_SPLIT(@TEXT, ',')) nums
      ON t.id = nums.num;
    
    0 讨论(0)
  • 2020-11-27 06:27

    The following works:

    declare @parStoreNo As varchar(8000) = '1,2,3,4'        
    CREATE TABLE #parStoreNo (StoreNo INT)-- drop #parStoreNo
    declare @temptable VARCHAR(1000) = @parStoreNo
    declare @SQL VARCHAR(1000) 
    SELECT @SQL = CONVERT(VARCHAR(1000),' select ' + REPLACE(ISNULL(@temptable,' NULL '),',', ' AS Col UNION ALL SELECT ')) 
    INSERT #parStoreNo (StoreNo)
    EXEC (@SQL)
    
    0 讨论(0)
  • 2020-11-27 06:30

    Possible duplicate of separate comma separated values and store in table in sql server.

    Please try a precise one from Comma-Delimited Value to Table:

    CREATE FUNCTION [dbo].[ufn_CSVToTable] ( @StringInput VARCHAR(8000), @Delimiter nvarchar(1))
    RETURNS @OutputTable TABLE ( [String] VARCHAR(10) )
    AS
    BEGIN
    
        DECLARE @String    VARCHAR(10)
    
        WHILE LEN(@StringInput) > 0
        BEGIN
            SET @String      = LEFT(@StringInput, 
                                    ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput) - 1, -1),
                                    LEN(@StringInput)))
            SET @StringInput = SUBSTRING(@StringInput,
                                         ISNULL(NULLIF(CHARINDEX(@Delimiter, @StringInput), 0),
                                         LEN(@StringInput)) + 1, LEN(@StringInput))
    
            INSERT INTO @OutputTable ( [String] )
            VALUES ( @String )
        END
    
        RETURN
    END
    GO
    

    Check the requirement in other way using XML:

    DECLARE @param NVARCHAR(MAX)
    SET @param = '1:0,2:1,3:1,4:0'
    
    SELECT 
         Split.a.value('.', 'VARCHAR(100)') AS CVS  
    FROM  
    (
        SELECT CAST ('<M>' + REPLACE(@param, ',', '</M><M>') + '</M>' AS XML) AS CVS 
    ) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)
    
    0 讨论(0)
  • 2020-11-27 06:31

    Try this code

     SELECT RTRIM(part) as part
        INTO Table_Name
            FROM dbo.splitstring(@Your_Comma_string,',')
    

    splitstring Function is as follows

    CREATE FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )
    RETURNS
     @returnList TABLE ([Name] [nvarchar] (500))
    AS
    BEGIN
    
     DECLARE @name NVARCHAR(255)
     DECLARE @pos INT
    
     WHILE CHARINDEX(',', @stringToSplit) > 0
     BEGIN
      SELECT @pos  = CHARINDEX(',', @stringToSplit)  
      SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)
    
      INSERT INTO @returnList 
      SELECT @name
    
      SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
     END
    
     INSERT INTO @returnList
     SELECT @stringToSplit
    
     RETURN
    END
    
    0 讨论(0)
  • 2020-11-27 06:41

    I am using XML Function as below...

    DECLARE @str VARCHAR(4000) = '6,7,7,8,10,12,13,14,16,44,46,47,394,396,417,488,714,717,718,719,722,725,811,818,832,833,836,837,846,913,914,919,922,923,924,925,926,927,927,928,929,929,930,931,932,934,935,1029,1072,1187,1188,1192,1196,1197,1199,1199,1199,1199,1200,1201,1202,1203,1204,1205,1206,1207,1208,1209,1366,1367,1387,1388,1666,1759,1870,2042,2045,2163,2261,2374,2445,2550,2676,2879,2880,2881,2892,2893,2894'
    
    Declare @x XML 
    
    select @x = cast('<A>'+ replace(@str,',','</A><A>')+ '</A>' as xml)
    
    select t.value('.', 'int') as inVal
    from @x.nodes('/A') as x(t)
    

    I prefer this because not need to create any separate function and proc. Also I don't have to opt dynamic SQL query which I prefer most. Convert Comma Separated String to Table

    0 讨论(0)
  • 2020-11-27 06:41
    DECLARE @str VARCHAR(4000) = '6,7,7,8,10,12,13,14,16,44,46,47,394,396,417,488,714,717,718,719,722,725,811,818,832'
    
    DECLARE @x XML 
    select @x = cast('<A>'+ replace(@str,',','</A><A>')+ '</A>' as xml)
    
    select t.value('.', 'int') as inVal
    from @x.nodes('/A') as x(t)
    
    0 讨论(0)
提交回复
热议问题