Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Regular expression in T-sql


Regular expression in T-sql

Author
Message
PradeepVallabh
PradeepVallabh
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 526
how do we use Regular expressions in T-sql...Do we need to deploy the assemblies to sql server???
Robin Sasson
Robin Sasson
Right there with Babe
Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)Right there with Babe (769 reputation)

Group: General Forum Members
Points: 769 Visits: 530
I'm sure that SQL Server does not support regular expressions within TSQL; I'd recommend you create a CLR.

Exclamation "Be brave. Take risks. Nothing can substitute experience." Exclamation
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16461 Visits: 13207
Robin is right, CLR is the way to go.
Example:
Regular Expressions Make Pattern Matching And Data Extraction Easier



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
manub22
manub22
SSC-Enthusiastic
SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)SSC-Enthusiastic (137 reputation)

Group: General Forum Members
Points: 137 Visits: 1861
What kinds of patterns you want to search for?

If not complex then you can use some expressions with LIKE clause.
Like if you want to search Canada ZIP codes, which are in following format 'X0X0X0', so for this you can use following query:

WHERE column_name LIKE '[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]'


Otherwise, like as other said, for complex searches you need CLR.


~manoj | LinkedIn | Facebook | YouTube
Blog: SQLwithManoj.com

Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
There are two T-SQL features which support limited regular expression functionality:
LIKE operator and PATINDEX function.
Depends of what you really need it for.
Note, the more complicated operation involved (eg. pattern match and replace), the length of the string and some other factors, then more likely that CLR solution will produce better performance. Also, CLR will allow to use RegEx objects with all available functionality.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Alan.B
Alan.B
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2242 Visits: 7426
CELKO (9/15/2012)

...And then there is a high cost of CLR. .


Hogwash. There is nothing "high cost" about a well written CLR.

Avoid this kludge if you can.

There's nothing "kludgy" about using a CLR to solve a problem that can't be solved by using T-SQL. Period.

I concur with the previous posters who suggest using a CLR for regular expressions.

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Ben Teraberry
Ben Teraberry
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1105 Visits: 1199
Microsoft invested a huge amount of effort/time/money into CLR to give us tremendous capabilities. I have been told by someone close to the SQL dev team at MS that they are tremendously disappointed by the lack of adoption to the point they significantly curtailed planned efforts to make CLR even more robust. People like CELKO spread misinformation and outright lies about CLR to prevent more widespread adoption. Why they do such a thing is a guess, but I think it has something to do with not wanting to learn new things due to being stuck in the 80's (or 60's as the case may be.) This may seem harsh, but I think it's fantastically unwarranted to lie on a forum that's supposed to help younger developers get the truth about SQL Server.

└> bt


Forum Etiquette: How to post data/code on a forum to get the best help
Alan.B
Alan.B
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2242 Visits: 7426
bteraberry (9/28/2012)
Microsoft invested a huge amount of effort/time/money into CLR to give us tremendous capabilities. I have been told by someone close to the SQL dev team at MS that they are tremendously disappointed by the lack of adoption to the point they significantly curtailed planned efforts to make CLR even more robust. People like CELKO spread misinformation and outright lies about CLR to prevent more widespread adoption. Why they do such a thing is a guess, but I think it has something to do with not wanting to learn new things due to being stuck in the 80's (or 60's as the case may be.) This may seem harsh, but I think it's fantastically unwarranted to lie on a forum that's supposed to help younger developers get the truth about SQL Server.


Well said.

I am by no means a Microsoft Koolaid drinker: Microsoft does some things I don't agree with but introducing CLRs was absolutely a great move on their part.

-- Alan Burstein



Best practices for getting help on SQLServerCentral
Need to split a string? Try DelimitedSplit8K or DelimitedSplit8K_LEAD (SQL 2012+)
Need a pattern-based splitter? Try PatternSplitCM
Need to remove or replace those unwanted characters? Try PatExclude8K and PatReplace8K.

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45147 Visits: 39923
XMLSQLNinja (9/27/2012)
CELKO (9/15/2012)

...And then there is a high cost of CLR. .


Hogwash. There is nothing "high cost" about a well written CLR.

Avoid this kludge if you can.

There's nothing "kludgy" about using a CLR to solve a problem that can't be solved by using T-SQL. Period.

I concur with the previous posters who suggest using a CLR for regular expressions.


As with all else, "It Depends". In the case of RegEx CLR vs Like and PatIndex... if you can actually work it out with Like, PatIndex, or even CharIndex, it will usually be faster than making a call to a RegEx CLR. A couple of us just went through all of this on another thread a couple of months ago and the LIKE expressions blew the doors off the RegEx CLR.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search