SQL handles data with commas

Source: Internet
Author: User

The purpose of this blog is to deal with the use of SQL to convert data with commas into rows in a table and to stitch rows from a table into data with commas.

The specific code below is written in the comments.

--convert data with commas to a table storage ID plus data godeclare @Text VARCHAR SET @Text = ' test1,test2 ' DECLARE @xml xmlset @xml = CONVERT (XML, ' <a > ' + REPLACE (@Text, ', ', ' </a><a> ') + ' </a> ') CREATE TABLE #TMP1 (iID varchar, stext varchar (200)) INSERT into #TMP1SELECT NEWID (), B.valuefrom (select @xml as ' xml ') ACROSS APPLY (select Tb.a.value ('. ', ' VARCHAR ") as Value from  a.xml.nodes ('/A ') as Tb (a)) bselect * from #TMP1GO--the data of a column in a TABLE is spelled in a comma-delimited form the go    CREATE TABLE #TMP2 (  IID Int,stext VARCHAR) insert into #TMP2 (IID, Stext) VALUES (1,  ' test1 ') and insert into #TMP2 (IID, stext) VALUES ( 1,  ' test2 ');D eclare @TMP2RES VARCHAR () Set @TMP2RES = (SELECT stext+ ', ' from #TMP2 for XML PATH (")) Set @TMP2RES = SUBSTRING (@TMP2RES, 0,len (@TMP2RES)-1) SELECT @TMP2RESGO

Run results


SQL handles data with commas

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.