Rumah  >  Artikel  >  pangkalan data  >  sql TEXT、NTEXT字段拆分的问题

sql TEXT、NTEXT字段拆分的问题

WBOY
WBOYasal
2016-06-07 17:46:581133semak imbas

 

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

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn