我写了一个函数,它返回一个带有单列的表,其中数据分隔为“|”。
例如,如果您将参数传递给函数funsplit('SAGAR|INDIA|MUMBAI','|')
它返回为
Item<Column Name>
SAGAR
INDIA
MUMBAI
我想要它作为
COLUMN 1 COLUMN2 COLUMN3
-----------------------------
SAGAR INDIA MUMBAI
这是我的功能
ALTER FUNCTION [dbo].[funSplit]
(
@sInputList Varchar(8000), -- List of delimited items
@sDelimiter VarChar(8000) = ',' -- delimiter that separates items
)
Returns @List Table (item VarChar(8000))
Begin
Declare @sItem VarChar(8000)
While CharIndex(@sDelimiter,@sInputList,0) <> 0
Begin
Select
@sItem=RTrim(LTrim(SubString(@sInputList,1,CharIndex(@sDelimiter,@sInputList,0)-1))),
@sInputList=RTrim(LTrim(SubString(@sInputList,CharIndex(@sDelimiter,@sInputList,0)+Len(@sDelimiter),Len(@sInputList))))
If Len(@sItem) > 0
Insert Into @List Select @sItem
End
If Len(@sInputList) > 0
Insert Into @List Select @sInputList -- Put the last item in
Return
End