sql server 表值函数将字符串转为列

时间:2022-06-19 13:29:37
1.标题:将字符串@str变成表中的列

DECLARE @str VARCHAR(8000)
SET @str='a,bb,cc,dd,ff,gg'

SELECT @str,* FROM Func_SplitStrToTable(@str)

sql  server 表值函数将字符串转为列

 

代码如下:
SET
QUOTED_IDENTIFIER ONSET ANSI_NULLS ON
GO
CREATE FUNCTION [dbo].[Func_SplitStrToTable] ( @str VARCHAR(8000) )
RETURNS @tableName TABLE ( Col VARCHAR(100) )
AS --该函数用于把一个用逗号分隔的多个数据字符串变成一个表的一列,例如字符串'1,2,3,4,5' 将转换成一个表

BEGIN

SET @str = @str + ','

DECLARE @insertStr VARCHAR(100) --截取后的第一个字符串

DECLARE @newstr VARCHAR(8000) --截取第一个字符串后剩余的字符串

SET @insertStr = LEFT(@str, CHARINDEX(',', @str) - 1)

SET @newstr = STUFF(@str, 1, CHARINDEX(',', @str), '')

INSERT @tableName
VALUES ( @insertStr )

WHILE ( LEN(@newstr) > 0 )
BEGIN

SET @insertStr = LEFT(@newstr, CHARINDEX(',', @newstr) - 1)

INSERT @tableName
VALUES ( @insertStr )

SET @newstr = STUFF(@newstr, 1, CHARINDEX(',', @newstr), '')

END

RETURN

END
GO



 

/*  
2.标题:按某字段合并字符串之一(简单合并)
3.作者:(十八年风雨,守得冰山雪莲花开)
4.时间:2008-11-06
5.地点:广东深圳
6.
7.描述:将如下形式的数据按id字段合并value字段。
8.id value
9.----- ------
10.1 aa
11.1 bb
12.2 aaa
13.2 bbb
14.2 ccc
15.需要得到结果:
16.id value
17.------ -----------
18.1 aa,bb
19.2 aaa,bbb,ccc
20.即:group by id, 求 value 的和(字符串相加)
21.
*/
22.--1、sql2000中只能用自定义的函数解决
23.create table tb(id int, value varchar(10))
24.insert into tb values(1, 'aa')
25.insert into tb values(1, 'bb')
26.insert into tb values(2, 'aaa')
27.insert into tb values(2, 'bbb')
28.insert into tb values(2, 'ccc')
29.go
30.
31.create function dbo.f_str(@id varchar(10)) returns varchar(1000)
32.as
33.begin
34. declare @str varchar(1000)
35. select @str = isnull(@str + ',' , '') + cast(value as varchar) from tb where id = @id
36. return @str
37.end
38.go
39.
40.--调用函数
41.select id , value = dbo.f_str(id) from tb group by id
42.
43.drop function dbo.f_str
44.drop table tb
45.
46.
47.--2、sql2005中的方法
48.create table tb(id int, value varchar(10))
49.insert into tb values(1, 'aa')
50.insert into tb values(1, 'bb')
51.insert into tb values(2, 'aaa')
52.insert into tb values(2, 'bbb')
53.insert into tb values(2, 'ccc')
54.go
55.
56.select id, [value] = stuff((select ',' + [value] from tb t where id = tb.id for xml path('')) , 1 , 1 , '')
57.from tb
58.group by id
59.
60.drop table tb
61.
62.
63.--3、使用游标合并数据
64.create table tb(id int, value varchar(10))
65.insert into tb values(1, 'aa')
66.insert into tb values(1, 'bb')
67.insert into tb values(2, 'aaa')
68.insert into tb values(2, 'bbb')
69.insert into tb values(2, 'ccc')
70.go
71.declare @t table(id int,value varchar(100))--定义结果集表变量
72.--定义游标并进行合并处理
73.declare my_cursor cursor local for
74.select id , value from tb
75.declare @id_old int , @id int , @value varchar(10) , @s varchar(100)
76.open my_cursor
77.fetch my_cursor into @id , @value
78.select @id_old = @id , @s=''
79.while @@FETCH_STATUS = 0
80.begin
81. if @id = @id_old
82. select @s = @s + ',' + cast(@value as varchar)
83. else
84. begin
85. insert @t values(@id_old , stuff(@s,1,1,''))
86. select @s = ',' + cast(@value as varchar) , @id_old = @id
87. end
88. fetch my_cursor into @id , @value
89.END
90.insert @t values(@id_old , stuff(@s,1,1,''))
91.close my_cursor
92.deallocate my_cursor
93.
94.select * from @t
95.drop table tb