How to split string and insert values into table in SQL Server

前端 未结 8 638
青春惊慌失措
青春惊慌失措 2020-11-29 12:36

I have a string like this:

72594206916,2,1/2/08,Tacoma,WA:72594221856,5,5/7/13,San Francisco,CA:72594221871,99,12/30/12,Dallas,TX

This is b

8条回答
  •  佛祖请我去吃肉
    2020-11-29 12:48

    Function Definition by Romil
    Create this Function in your Sql Server

    CREATE FUNCTION Split (
          @InputString                  VARCHAR(8000),
          @Delimiter                    VARCHAR(50)
    )
    
    RETURNS @Items TABLE (
          Item                          VARCHAR(8000)
    )
    
    AS
    BEGIN
          IF @Delimiter = ' '
          BEGIN
                SET @Delimiter = ','
                SET @InputString = REPLACE(@InputString, ' ', @Delimiter)
          END
    
          IF (@Delimiter IS NULL OR @Delimiter = '')
                SET @Delimiter = ','
    
    --INSERT INTO @Items VALUES (@Delimiter) -- Diagnostic
    --INSERT INTO @Items VALUES (@InputString) -- Diagnostic
    
          DECLARE @Item                 VARCHAR(8000)
          DECLARE @ItemList       VARCHAR(8000)
          DECLARE @DelimIndex     INT
    
          SET @ItemList = @InputString
          SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
          WHILE (@DelimIndex != 0)
          BEGIN
                SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)
                INSERT INTO @Items VALUES (@Item)
    
                -- Set @ItemList = @ItemList minus one less item
                SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
                SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
          END -- End WHILE
    
          IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString
          BEGIN
                SET @Item = @ItemList
                INSERT INTO @Items VALUES (@Item)
          END
    
          -- No delimiters were encountered in @InputString, so just return @InputString
          ELSE INSERT INTO @Items VALUES (@InputString)
    
          RETURN
    
    END -- End Function
    GO
    

    Pass Your Parameteres
    Since it is a Table Function you will SELECT * FROM this_Function like you would do with a table

    declare @testString varchar(100)
    set @testString = '72594206916,2,1/2/08,Tacoma,WA:72594221856,5,5/7/13,San Francisco,CA:72594221871,99,12/30/12,Dallas,TX'
    
    SELECT * FROM Split(@testString, ',')
    

    Result Set

    Item
    72594206916
    2
    1/2/08
    Tacoma
    WA:72594221856
    5
    5/7/13
    San Francisco
    CA:72594221871
    99
    12/30/12
    Dallas
    

    Your Existing Code

    select *
    from dbo.SplitString(@testString, ':')
    

    The Second Parameter needs to be the deliminator, Since you are Passing : as the second parameter it is breaking you string where ever it finds : in you passed string which is obviously in 2 places and you get back 3 values/Strings in the result set

                              String1/Value1                             String2/Value2                    String3/Value3
    set @testString = '72594206916,2,1/2/08,Tacoma,WA  :   72594221856,5,5/7/13,San Francisco,CA  :  72594221871,99,12/30/12,Dallas,TX'
    

提交回复
热议问题