SQL character regular match

Source: Internet
Author: User
Method1:
Declare   @ Tempstr   Varchar ( 100 )
Declare   @ Tempret   Varchar ( 20 )
Select   @ Tempstr   =   ' 0102017010101162873.4 Deng Xiangjiang 56.5064.9512 '
Select   @ Tempret   =   Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( Replace ( @ Tempstr , ' 0 ' , '' ), ' 1 ' , '' ), ' 2 ' , '' ), ' 3 ' , '' ), ' 4 ' , '' ), ' 5 ' , '' ), ' 6 ' , '' ), ' 7 ' , '' ), ' 8 ' , '' ), ' 9 ' , '' ), ' . ' , '' )
Print   @ Tempret

Method2:
Print DBO. regexreplace ( ' 0102017010101162873.4 Deng Xiangjiang 56.5064.9512 ' , ' [0-9.] ' , '' , 1 , 1 )

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 -- Ignore size?
)
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
Go

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.