पते के साथ मेल के लिए विशिष्ट नियमों वाली तालिका बनाने का एक उदाहरण यहां दिया गया है (regexp)। फिर एसक्यूएल क्वेरी पैटर्न (Regexp) के साथ एक उदाहरण। इससे आपको वह करने में सक्षम होना चाहिए जो आप चाहते हैं
regexp के साथ तालिका बनाएं
create table Contacts (
FirstName nvarchar(30),
LastName nvarchar(30),
EmailAddress nvarchar(30) CHECK (dbo.RegExMatch('[a-zA-Z0-9_\-]example@sqldat.com([a-zA-Z0-9_\-]+\.)+(com|org|edu|nz)', EmailAddress)=1),
USPhoneNo nvarchar(30) CHECK (dbo.RegExMatch('\([1-9][0-9][0-9]\) [0-9][0-9][0-9]\-[0-9][0-9][0-9][0-9]', UsPhoneNo)=1))
INSERT INTO [talend].[dbo].[Contacts]
([FirstName]
,[LastName]
,[EmailAddress]
,[USPhoneNo])
VALUES
('Hallam'
,'Amine'
,'example@sqldat.com’
,'0129-2090-1092')
,( 'encoremoi'
,'nimportequoi'
,'example@sqldat.com'
,'(122) 190-9090')
GO
regexp के साथ अनुरोध sql निष्पादित करें
SELECT [FirstName]
,[LastName]
,[EmailAddress]
,[USPhoneNo]
FROM [talend].[dbo].[Contacts]
where [talend].[dbo].RegExMatch([EmailAddress],'[a-zA-Z0-9_\-]example@sqldat.com([a-zA-Z0-9_\-]+\.)+(com|org|edu|nz|au)') = 1
फ़ंक्शन कोड
using System;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;
public partial class RegExBase
{
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static int RegExMatch( string matchString , string pattern)
{
Regex r1 = new Regex(pattern.TrimEnd(null));
if (r1.Match(matchString.TrimEnd(null)).Success == true)
{
return 1 ;
}
else
{
return 0 ;
}
}
};