我想分割功能像兩個單獨的列與給定的輸入字符串。分割函數逗號分隔符SQL Server
如果我執行這個功能,我得到行的值列表像
select *
from dbo.Split ('sunday,9,monday,8,tuesday,9, wednesday 8, thursday 8,friday,9, saturday,8')
輸出像行值
sunday
9
monday
8
tuesday
9
wednesday
8
thursday
8
friday
9
saturday
8
我分割功能是:
create function dbo.Split
(
@String varchar(max)
)
returns @SplittedValues table
(
Id varchar(50)
)
as
begin
declare @SplitLength int, @Delimiter varchar(5)
set @Delimiter = ','
while len(@String) > 0
begin
select @SplitLength = (case charindex(@Delimiter,@String) when 0 then
len(@String) else charindex(@Delimiter,@String) -1 end)
insert into @SplittedValues
select substring(@String,1,@SplitLength)
select @String = (case (len(@String) - @SplitLength) when 0 then ''
else right(@String, len(@String) - @SplitLength - 1) end)
end
return
end
但我需要輸出像兩列天(列表中的第一個參數),小時,
+-----------+-------+
| Days | Hours |
+-----------+-------+
| Sunday | 9 |
| Monday | 8 |
| Tuesday | 9 |
| Wednesday | 8 |
| Thursday | 8 |
| Friday | 9 |
| saturday | 8 |
+-----------+-------+
我該如何解決這個問題?
在這裏查看各種方法,將CSV轉換爲行,並進行性能比較 – 2015-04-05 02:47:23
@Fireblade check where?我沒有看到鏈接 – 2015-04-05 03:51:39
@ t-clausen.dk - 哎呀錯過添加鏈接http://sqlperformance.com/2012/07/t-sql-queries/split-strings – 2015-04-05 04:15:06