Regular Expression functions in the database

Source: Internet
Author: User

Regular Expression functions in the database

Create   Function DBO. regexreplace
(
@ Source   Varchar ( 5000 ), -- Original string
@ Regexp   Varchar ( 1000 ), -- Regular Expression
@ Replace   Varchar ( 1000 ), -- Replacement value
@ Globalreplace   Bit   =   0 , -- Whether it is a global replacement
@ Ignorecase   Bit   =   0 -- Case Insensitive?
)
Returns   Varchar ( 1000 ) As
Begin
Declare   @ HR   Integer
Declare   @ Objregexp   Integer
Declare   @ Result   Varchar ( 5000 )

Exec   @ HR   = Sp_oacreate ' VBScript. Regexp ' , @ Objregexp Output
If   @ HR   <>   0   Begin
Exec   @ HR   = Sp_oadestroy @ Objregexp
Return   Null
End
Exec   @ HR   = Sp_oasetproperty @ Objregexp , ' Pattern ' , @ Regexp
If   @ HR   <>   0   Begin
Exec   @ HR   = Sp_oadestroy @ Objregexp
Return   Null
End
Exec   @ HR   = Sp_oasetproperty @ Objregexp , ' Global ' , @ Globalreplace
If   @ HR   <>   0   Begin
Exec   @ HR   = Sp_oadestroy @ Objregexp
Return   Null
End
Exec   @ HR   = Sp_oasetproperty @ Objregexp , ' Ignorecase ' , @ Ignorecase
If   @ HR   <>   0   Begin
Exec   @ HR   = Sp_oadestroy @ Objregexp
Return   Null
End  
Exec   @ HR   = Sp_oamethod @ Objregexp , ' Replace ' , @ Result Output, @ Source , @ Replace
If   @ HR   <>   0   Begin
Exec   @ HR   = Sp_oadestroy @ Objregexp
Return   Null
End
Exec   @ HR   = Sp_oadestroy @ Objregexp
If   @ HR   <>   0   Begin
Return   Null
End

Return   @ Result
End
Example

Select DBO. regexreplace (u_account, '[| = | region |-| [|] |: | [|. |/|' |? | "|! | □| _ |] | "|. | ^ |♂| + | ~ | ']', '', 1, 1) from userinfo where (u_account like '% [^ a-zA-Z0-9] % ')

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.