(я предположил, что ваши данные будут больше 4000 каратов.)
Функция разделения:
CREATE FUNCTION dbo.Split
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(2000)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
И функция разнесения:
CREATE FUNCTION dbo.*tableName*_Explode
(
@id int
)
RETURNS @RtnValue table
(
Id int,
Data NVARCHAR(2000)
)
AS
BEGIN
DECLARE @LINEBREAK AS varchar(2)
SET @LINEBREAK = CHAR(13) + CHAR(10)
DECLARE @short_text NVARCHAR(2000)
DECLARE @short_text_length INT
DECLARE @sub_length INT
SET @sub_length = 2000
DECLARE @sub_index INT
SET @sub_index = 1
BEGIN
SET @short_text = (select SUBSTRING(*tableData*,@sub_index,@sub_length) from *tableName* WHERE id = @id)
SET @short_text_length = DATALENGTH(@short_text)
WHILE (@short_text_length > 0)
BEGIN
Insert Into @RtnValue (id,data)
SELECT @id, @short_text
SET @sub_index = (@sub_index + @sub_length)
SET @short_text = (select SUBSTRING(*tableData*,@sub_index,@sub_length) from *tableName* WHERE id = @id)
SET @short_text_length = DATALENGTH(@short_text)
END
IF (SELECT COUNT(*) FROM @RtnValue) = 0
Insert Into @RtnValue (id,data)
SELECT @id, ''
END
RETURN
END