SQL 2005 Character function instance and application example
Use Demo
Go
/*
Extract the value from the column string of the table code into the record table
The character type in string is
dsddddd,2222222,222221,3
The last one was labeled for Biaoji in the record table
The value corresponding to the record table is divided by ', ' in the preceding
*/
Go
drop proc Proc_split_code
Go
Create proc Proc_split_code
As
Begin
SET NOCOUNT ON
DECLARE @count INT--Number of bars
DECLARE @index INT--Variable
Set @index = 1--Default
Select @count = count (*) from code
--print @count
while (@index <= @count)
Begin
DECLARE @biaoji INT--Mark
declare @string nvarchar (1000)--string
DECLARE @temp INT--The position of the separator
DECLARE @star INT--Start position
declare @code nvarchar (100)--
Set @star = 0
Select @string =reverse (String)
From (
Select Row_number () Over (order by string) as rownumber,* from code
) as a
where RowNumber between @index and @index
Set @temp =charindex (', ', @string, @star)
Set @biaoji = substring (@string, @star, @temp)
Print @biaoji
Set @string = Reverse (@string)
Set @temp =charindex (', ', @string, @star)
Set @star = 0
while (@temp >0)
Begin
Set @temp =charindex (', ', @string, @star)
--print @star
--print @temp
If @temp >0
Begin
Set @code =substring (@string, @star, @temp-@star)
Print @code
--INSERT into the appropriate table
INSERT into record (Biaoji,value,time)
VALUES (@biaoji, @code, GETDATE ())
End
Set @star = @temp +1
End
--print @index
Print @string
Set @index = @index +1
End
End
Go
EXEC Proc_split_code