views:

70

answers:

3

I need to extract something from a long piece of text across lots of db rows, in a Microsoft SQL Server database.

I could write a script to loop through and extract it, but I was hoping there was nice simple way I can do some SQL like:

SELECT IpAddress = matchFirst('RegEx',ColName)
FROM table
WHERE conditions


I've looked about but all I'm finding is unclear long-winded ramblings about using regex in the where clause and CLR UDFs and stuff - but all I want is a simple "insert regex here" answer.

Anyone ideas?

A: 

There isn't support for regular expressions built in to TSQL, so unfortunately there isn't an immediate way to just do it.

So you could either:
1) write a UDF function that performs string manipulation to try to parse the IPAddress out of a supplied string (i.e. using the functionality TSQL does support like PATINDEX, SUBSTRING etc) - if you didn't want to use SQLCLR for whatever reason

2) use SQL CLR to allow you to run regular expressions in a query - would need a .NET assembly to do the reg ex stuff, that can then be hooked into and called directly from your SQL query. The code to do this is already out there in a number of places. Apologies if this is one place you already looked, but have a look at this MSDN blog post - that gives the .NET code for the regex stuff, and an example of how to use it. And also this MSDN article on How to create and run a CLR SQL Server User-Defined Function

AdaTheDev
Well in this case I could have used PATINDEX+SUBSTRING if MS could implement a real Trim function (that removed all whitespace, not just space), but it'd still be nice to have a general use solution. If I can't just include a function in my SQL, I'll just go do a script anyway.
Peter Boughton
+1  A: 

If you're looking for a simple solution I would suggest using the SQL# library which basically contains the UDF you need and you'll find referenced elsewhere.

Once that's installed (it's reasonably painless to install) you will find a function called RegEx_MatchSimple which I believe is what you need.

Joel Mansford
Since the functionality isn't built-in this might be the best/only option... I'll install it locally and have a fiddle, then see if I can convince the DBA to let us have it on all the servers.
Peter Boughton
A: 

i know sql has at least some slight reg-ex compatibility, since you can do the following (sql 2005, 2008)

select * from table where CharField like '[a-z]%'

perhaps if you tell exactly what you need your regex to do someone might be able to give you a sql equivilent.

DForck42
For the specific example that prompted this question, I needed to extract IP addresses from a table where each record has an ~8kb text field (debugging info from badly written error logging software). I've solved that with workarounds, so now I'm just after a general solution for the next time I have to do something similar.
Peter Boughton