Using regular expressions in SQL Server

Source: Internet
Author: User
Tags ole

One, check the configuration, if ' Show advanced options ', ' Ole Automation procedures ', ' Ad Hoc distributed Queries ' config_value and Run_value is not 1, Two scripts are required to execute.

Second, execute the script, open SQL Server access block to the component.

Use master
GO
sp_configure ' show advanced options ', 1;
GO
RECONFIGURE;
GO
sp_configure ' Ole Automation procedures ', 1;
GO
RECONFIGURE;
GO
sp_configure ' Ad Hoc distributed Queries ', 1;
GO
RECONFIGURE;
GO

Third, create the function (except for the database name and the name of the function will remain unchanged)

Use edrmsofficedb;--(database name)
GO
If object_id (N ' dbo. Reg ') is not null--(function name)
Drop function dbo. Reg;
GO
CREATE FUNCTION Reg
(
@pattern varchar (2000),
@matchstring varchar (8000)
)
returns int
As
Begin
DECLARE @objRegexExp int
DECLARE @strErrorMessage varchar (255)
DECLARE @hr int, @match bit
EXEC @hr = sp_OACreate ' VBScript.RegExp ', @objRegexExp out
If @hr = 0
EXEC @hr = sp_OASetProperty @objRegexExp, ' Pattern ', @pattern
If @hr = 0
EXEC @hr = sp_OASetProperty @objRegexExp, ' IgnoreCase ', 1
If @hr = 0
EXEC @hr = sp_OAMethod @objRegexExp, ' Test ', @match out, @matchstring
If @hr <>0
Begin
return null
End
exec sp_OADestroy @objRegexExp
Return @match
End

Iv. Testing

The query field contains records other than letters and numbers:

SELECT CategoryId from Category WHERE dbo. Reg (' ^[a-za-z0-9]+$ ', CategoryId) =0

Using regular expressions in SQL Server

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.