Application of character functions in SQL Server 2005 _mssql2005

Source: Internet
Author: User
Copy Code code as follows:

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
Related Article

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.