开发者

How to separate the string value and create temp to store separate string in temp table

开发者 https://www.devze.com 2022-12-19 05:18 出处:网络
I am using SQL Server 2005. I have created a table with the columns ID and Courses. Records are as follows:

I am using SQL Server 2005.

I have created a table with the columns ID and Courses. Records are as follows:

ID      Courses

1.      Java, ASP.Net, C#

2.      Java

3.      Java, C#

4.      html

The Courses column is of type varchar and the values in it are comma separated. I want to separate every word and store it in another or temp. table.

Output must be like this:

ID &nb开发者_StackOverflow社区sp;    Courses

1.      Java

2.      ASP.Net

3.      C#

4.      html

Here ID is not important.

Thank you.


see my previous answer to this

this is the best source:

http://www.sommarskog.se/arrays-in-sql.html

create a split function, and use it like:

SELECT
    *
    FROM YourTable  y
    INNER JOIN dbo.splitFunction(@Parameter) s ON y.ID=s.Value

I prefer the number table approach

For this method to work, you need to do this one time table setup:

SELECT TOP 10000 IDENTITY(int,1,1) AS Number
    INTO Numbers
    FROM sys.objects s1
    CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)

Once the Numbers table is set up, create this function:

CREATE FUNCTION [dbo].[FN_ListToTable]
(
     @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
    ,@List     varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN 
(

    ----------------
    --SINGLE QUERY-- --this will not return empty rows
    ----------------
    SELECT
        ListValue
        FROM (SELECT
                  LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                  FROM (
                           SELECT @SplitOn + @List + @SplitOn AS List2
                       ) AS dt
                      INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                  WHERE SUBSTRING(List2, number, 1) = @SplitOn
             ) dt2
        WHERE ListValue IS NOT NULL AND ListValue!=''

);
GO 

You can now easily split a CSV string into a table and join on it:

select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')

OUTPUT:

ListValue
-----------------------
1
2
3
4
5
6777

(6 row(s) affected)

To make what you need work, use CROSS APPLY:

DECLARE @tbl_A table (RowID int, RowValue varchar(500))
DECLARE @tbl_b table (RowID int identity, RowValue varchar(500))

INSERT INTO @tbl_A VALUES (1, 'Java, ASP.Net, C#')
INSERT INTO @tbl_A VALUES (2, 'Java')
INSERT INTO @tbl_A VALUES (3, 'Java, C#')
INSERT INTO @tbl_A VALUES (4, 'html')

INSERT INTO @tbl_b (RowValue)
SELECT DISTINCT
    st.ListValue
    FROM @tbl_A 
        CROSS APPLY  dbo.FN_ListToTable(',',RowValue) AS st
    ORDER BY st.ListValue

SELECT * FROM @tbl_b ORDER BY RowID

OUTPUT:

RowID       RowValue
----------- --------------
1           ASP.Net
2           C#
3           html
4           Java

(4 row(s) affected)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号