SQL string split to column, SQL column to string

Source: Internet
Author: User
Tags rtrim

DECLARE @strVar varchar (2000)
DECLARE @ResultVar varchar (2000)
Set @ResultVar = ' [Pancake],[Dim sum],[Seafood],[bar],[floor],[tea],[pigeon],[roasted],[oyster],[Set banquet],[Simmer soup],[Chinese chef] '
SELECT * from Sys_fn_get_tablefromstringsplit (@strVar, ', ')

--result

Id Flowid

[Pizza] 1
[Dim sum] 2
[Seafood] 3
[Bar] 4
[Floor Area] 5
[Tea] 6
[Pigeon] 7
[Burning Flavor] 8
[Raw Oyster] 9
[Package Banquet] 10
[Simmer soup] 11
[Chinese Kitchen] 12

--diy
Set @ResultVar =stuff ((select ', ' + ' sum (' + p.id+ ') as ' +p.id
From Sys_fn_get_tablefromstringsplit (@strVar, ', ') p for XML Path (")), 1, 1, ')

Select @ResultVar

--result

SUM ([pizza]) as [Pizza],sum ([dim sum]) As [Dim Sum],sum ([seafood]) as [Seafood],sum ([bar]) as [Bar],sum ([floor]) as [floor area]

--string splitting into a table function

Create function [dbo]. [Sys_fn_get_tablefromstringsplit]
(
@Strings varchar (max)--the string to be split into a table may be divided by more than 8000 data
@Spliter varchar (10)--delimiter
)
Returns @Table Table (Id varchar (), Flowid int identity (+))
As
Begin
declare @index int, @tempValue varchar (50)

while (1 = 1)
Begin
Set @index = charindex (@Spliter, @Strings);
if (@index > 0)
Begin
Set @tempValue = substring (@Strings, 1, @index);
Set @Strings = substring (@Strings, @index + 1, len (@Strings) [email protected]);
Set @tempValue = replace (LTrim (RTrim (@tempValue)), @Spliter, ");
if (len (@tempValue) > 0)
Begin
INSERT INTO @Table
Values
(
@tempValue
);
End
End
Else
Begin
Set @tempValue = LTrim (RTrim (@Strings));
if (len (@tempValue) > 0)
Begin
INSERT INTO @Table
Values
(
@tempValue
);
End
Break
End
End
Return
/*********************************demo*****************************************

SELECT * FROM dbo. Sys_fn_get_tablefromstringsplit (' 1,2,3,,4,5,6 ', ', ');
SELECT * FROM dbo. Sys_fn_get_tablefromstringsplit (' 1 2 3,,4 5,6 ', ');
SELECT * FROM dbo. Sys_fn_get_tablefromstringsplit (' 1,2;3,,4,5;6 ', '; ');

*******************************************************************************/
End

SQL string split to column, SQL column to string

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.