CREATE FUNCTION Dbo.sf_ds_splitnvarchar
(
@strValues nvarchar (4000)
)
RETURNS @tblStrList TABLE (id int identity (), value nvarchar (4000))
As
BEGIN
declare @strTmp nvarchar (4000)
DECLARE @intPos int
Select @strValues =ltrim (RTrim (@strValues))
While @strValues <> '
Begin
Select @intPos =case when charindex (', ', @strValues) =0 then Len (@strValues) Else charindex (', ', @strValues)-1 end
Select @strTmp =ltrim (RTrim (substring (@strValues, 1, @intPos))
Select @strValues =ltrim (RTrim (substring (@strValues, @intPos +2,len (@strValues)))
INSERT into @tblStrList values (@strTmp)
End
RETURN
END
--Select value from Dbo.sf_ds_splitnvarchar (' A,b,c ')
--"Method two"
CREATE FUNCTION dbo.sf_splitvarchar_sign
(
@strValues varchar (8000),--string to be split
@Sign char (1)--delimiter
)
RETURNS @tblStrList TABLE (Zid int identity (), value varchar (8000))
as
BEGIN
DECLARE @strTmp varchar (8000),
@intPos int
Select @strValues =ltrim (RTrim (@strValues))
If Right (@strVal ues,1) <> @Sign
Select @strValues = @strValues + @Sign
If @strValues <> @Sign
Begin
S Elect @intPos = charindex (@Sign, @strValues)
while @intPos > 0
Begin
Select @strTmp = LTrim (RTrim (Substri Ng (@strValues, 1, @intPos-1))
INSERT into @tblStrList values (@strTmp)
Select @strValues = LTrim (RTrim (substring (@strValues, @intPos + 1,len (@strValues))))
Select @intPos = charindex (@Sign, @strValues)
Select @intPos = IsNull (@intPos, 0)
End
End
retur N
END
--select value from Dbo.sf_splitvarchar_sign (' a,b,c ', ', ')
--a
--b
--c
SQL Server split string to column