欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

sql TEXT、NTEXT字段拆分的问题

程序员文章站 2024-04-05 19:59:54
...
sql TEXT、NTEXT字段拆分的问题

SET NOCOUNT ON
CREATE TABLE #T (C NTEXT)
GO
INSERT INTO #T VALUES('')
GO
CREATE TABLE #T2 (C CHAR(8))
GO

DECLARE @S1 VARCHAR(8000),@S2 VARCHAR(8000),@I INT
SET @S1=''
SET @S2=''
SET @I=1
WHILE @I1000
BEGIN
SET @S1=@S1+'A'+RIGHT('0000000'+CAST(@I AS VARCHAR(5)),6)+','
SET @S2=@S2+'B'+RIGHT('0000000'+CAST(@I AS VARCHAR(5)),6)+','
SET @I=@I+1
END

DECLARE @P BINARY(16)
SELECT @P = TEXTPTR(C) FROM #T
UPDATETEXT #T.C @P NULL NULL @S1
UPDATETEXT #T.C @P NULL NULL @S2
SELECT DATALENGTH(C) FROM #T

DECLARE @DL INT
SET @DL = (SELECT DATALENGTH(C) FROM #T)/2
SET @I=1
WHILE @I @DL
BEGIN
INSERT INTO #T2 SELECT SUBSTRING(C,@I,7) FROM #T
SET @I=@I+8
END

SELECT * FROM #T2

/*
功能:实现split功能的函数
*/

create function dbo.fn_split
(
@inputstr varchar(8000),
@seprator varchar(10)
)
returns @temp table (a varchar(200))
as

begin
declare @i int

set @inputstr = rtrim(ltrim(@inputstr))
set @i = charindex(@seprator, @inputstr)

while @i >= 1
begin
insert @temp values(left(@inputstr, @i - 1))

set @inputstr = substring(@inputstr, @i + 1, len(@inputstr) - @i)
set @i = charindex(@seprator, @inputstr)
end

if @inputstr '\'
insert @temp values(@inputstr)

return
end
go

--调用

declare @s varchar(1000)

set @s='1,2,3,4,5,6,7,8,55'

select * from dbo.fn_split(@s,',')

drop function dbo.fn_split