sqlserver2008 Split String _mssql2008

Source: Internet
Author: User
--Note: For example, the following data

ID Id_value
-----------------
1 ' aa,bb '
2 ' AA,BB,CC '
3 ' AAA,BBB,CCC '

--Convert to the following format
ID Id_value
-----------------
1 ' AA '
1 ' BB '
2 ' AA '
2 ' BB '
2 ' CC '
3 ' AAA '
3 ' BBB '
3 ' CCC '

--Code-------------------------------------------Go
Copy Code code as follows:

CREATE table Ta
(
ID int,
id_value varchar ()
)

INSERT INTO TA
Select 1, "AA,BB ' union All"
Select 2, ' AA,BB,CC ' union ALL
Select 3, ' AAA,BBB,CCC ' br>
Select
Top 4000 IX = identity (int, 1, 1)
to #tb
from sys.objects A, sys.objects b

select
ID,
Id_value = substring (id_value,ix,charindex (', ', Id_value + ', ', ix)-IX "
from TA as A, #tb as B
WHERE SUBSTRING (', ' + Id_value, IX, 1) = ', '

drop table ta
drop table #tb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.