Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Regular expression in T-sql Expand / Collapse
Author
Message
Posted Thursday, April 19, 2012 12:19 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, October 22, 2013 2:21 PM
Points: 55, Visits: 526
how do we use Regular expressions in T-sql...Do we need to deploy the assemblies to sql server???
Post #1286617
Posted Friday, April 20, 2012 1:56 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Friday, June 27, 2014 7:41 AM
Points: 739, Visits: 518
I'm sure that SQL Server does not support regular expressions within TSQL; I'd recommend you create a CLR.

"Be brave. Take risks. Nothing can substitute experience."
Post #1286924
Posted Wednesday, April 25, 2012 5:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:02 PM
Points: 13,538, Visits: 11,342
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 LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1289743
Posted Thursday, July 26, 2012 8:43 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 9:12 PM
Points: 86, Visits: 761
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
sqlwithmanoj.wordpress.com
Post #1335897
Posted Saturday, September 15, 2012 3:16 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 12:29 PM
Points: 1,945, Visits: 3,121
T-SQL has a limited regular expression ability in the LIKE predicate extensions. Look them up in Books on Line. Frankly, it has been strong enough for over 90% of the applications I have.

The ANSI Standard SQL has the SIMILAR TO predicate which is based on the POSIX standards. I do not know if MS will catch up

You can use CLR, but then you have to worry about maintaining every possible regular expression tools, since they are all a little different. And then there is a high cost of CLR. Avoid this kludge if you can.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1359845
Posted Tuesday, September 25, 2012 3:57 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:23 AM
Points: 2,873, Visits: 5,189
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1363896
Posted Thursday, September 27, 2012 11:29 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 6:17 AM
Points: 632, Visits: 2,951
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



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"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

My blog
Post #1365612
Posted Friday, September 28, 2012 2:08 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, November 14, 2014 4:49 PM
Points: 1,093, Visits: 1,175
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
Post #1366106
Posted Friday, September 28, 2012 5:10 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 6:17 AM
Points: 632, Visits: 2,951
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



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"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

My blog
Post #1366137
Posted Friday, September 28, 2012 9:04 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 12:06 PM
Points: 35,572, Visits: 32,164
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1366153
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse