-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstring_to_column.sql
More file actions
53 lines (41 loc) · 1.11 KB
/
string_to_column.sql
File metadata and controls
53 lines (41 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
--Convert string to column
use tempdb
go
/* @example :
use tempdb
go
select * from ( N'one/two/three/four/five', '/' )
*/
if object_id('STRING_TO_COLUMN') is not null
drop function STRING_TO_COLUMN;
create function STRING_TO_COLUMN( @str varchar(max), @delimeter char(1) )
returns @t table ( name nvarchar(max) )
as
BEGIN
declare @i int
set @i = CHARINDEX(@delimeter, @str)
while 1 = 1
begin
if @i != 0
begin
insert @t values ( left(@str, @i - 1) );
set @str = right( @str, len(@str) - @i )
set @i = charindex( @delimeter, @str )
continue;
end;
else
insert @t values(@str)
set @str = null
break;
end;
return
END
GO
--xml
DECLARE @xml as xml,@str as varchar(100),@delimiter as varchar(10)
SET @str='A,B,C,D,E'
SET @delimiter =','
SET @xml = cast(('<X>'+replace(@str,@delimiter ,'</X><X>')+'</X>') as xml)
SELECT N.value('.', 'varchar(10)') as value
FROM @xml.nodes('X') as T(N)
;