SQL 2005 Character function instance and application example

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.