SQL string splitting

Source: Internet
Author: User
There is such a string (China, People, country, modern industrialization process, country)

After processing it, it becomes (China, the country, the people, and the modern industrialization process)

 

The following is my method. Please give me some advice on anything wrong.

------------------Delete repeated characters

Declare @ V1 Nvarchar (100)
Declare @ V2 Nvarchar (100)
Declare @ V3 Nvarchar (100)
Declare @ Split Nvarchar (100)
-- N', China, China, People, country, modern industrialization process, country ,,,'
Set @ V1 = n', China, People, country, modern industrialization process, country ,,,'
Set @ V3 =''
Set @ Split = ','
/*
@ V1: stores temporary strings
@ V2: gets the strings in two separators.
@ V3: stores strings that are not repeated at the end
*/
If Left (@ V1, 1) <> @ split
Set @ V1 = @ split + @ V1
If Right (@ V1, 1) <> @ split
Set @ V1 = @ V1 + @ split
While Len (@ V1)> Len (@ split) * 2
Begin
Set @ V2 = Substring (@ V1, Len (@ split ), Charindex (@ Split, @ V1, Len (@ Split) + 1 ))
If Len (@ V2)> Len (@ Split) * 2
Begin
Set @ V2 = Right (@ V2, Len (@ V2)-1 );
If Ltrim ( Rtrim (@ V2) <> @ split
Set @ V3 = @ V3 + Ltrim ( Rtrim (@ V2 ));
Set @ V1 = Replace (@ V1, @ V2 ,'')
End
Else -- If a continuous delimiter exists, delete it.
Set @ V1 = Stuff (@ V1, Len (@ Split) + 1, Len (@ Split ),'')
End
Set @ V3 = Ltrim ( Rtrim (@ V3 ))
While (Left (@ V3, 1) = ',')
Set @ V3 = Ltrim ( Rtrim (Right (@ V3, Len (@ V3)-1 )))
While (Right (@ V3, 1) = ',')
Set @ V3 = Ltrim ( Rtrim ( Left (@ V3, Len (@ V3)-1 )))

select @ V3
----------------- extract Chinese

Declare@ V1 nvarchar (100)
Set@ V1 = n' AB 3D 3D % Block E // f456'
WhilePatindex(% [A-Z, 0-9, %] % ', @ V1)> 0
Begin
-- Set @ v2 = isnull (@ V2, '') + substring (@ V1, patindex ('% [0-9] %', @ V1), 1 );
Set@ V1 =Stuff(@ V1,Patindex('% [A-Z, 0-9, %,/] %', @ V1), 1 ,'')
End
Select@ V1

 

 

----------------- extract letters, number

Declare@ V1Nvarchar(100)
Set@ V1 = n' AB 3D 3D % Block E // f456'
WhilePatindex(% [^ A-Z] % ', @ V1)> 0
Begin
-- Set @ v2 = isnull (@ V2, '') + substring (@ V1, patindex ('% [0-9] %', @ V1), 1 );
Set@ V1 =Stuff(@ V1,Patindex('% [^ A-Z] %', @ V1), 1 ,'')
End
Select@ V1

 

 

-------Filter repeated characters

Declare @ V1 Nvarchar (100)
Declare @ V2 Nvarchar (100)
-- N' ,, the modern industrialization process country of the People's Republic of China ,,,'
Set @ V1 = n', China's People's Republic of China's modern industrialized process country ,,,'
While Len (@ V1)> 0
Begin
Set @ V2 = Isnull (@ V2, '') + Left (@ V1, 1)
Set @ V1 = Replace (@ V1, Left (@ V1, 1 ),'')
End
Select @ V2

Share:

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.