on above table, I need to extract only string from those rows of pattern which start from abc or xyz Those patter may be 1 or more time. If more then one then have to retrive rest of those pattern too. Start with same pattern as in where clause but end of that links are some time ", some time space, some time /> and some time end of line.
(comments are locked)
|
|
If you are running SQL Server 2005 and above, then the easies way will be CLR Implementation of RegEx. You can take a look on my post SQL Server 2005 and SQL Server 2008 Regular Expressions and pattern matching. There is also a sample solution available for download and this is easily solvable by RegEx expression. +1. Cannot agree more. Best solution in such scenarios is to use a CLR function.
Jun 20 '12 at 01:44 PM
Usman Butt
to use CLR I need to know VB and C# too. Or I can just use ms-sql?
Jun 20 '12 at 02:01 PM
munabhai
@munabhai Yes, you need to know VB and/or C#. However, in this case, because @Pavel's solution (in the post linked in his answer above) is complete and includes very detailed instructions on how to deploy the CLR solution to the SQL Server, all you really need is just a basic understanding of the syntax. Read through the article, follow the code along with instructions and you should be able to deploy his solution in the relatively short period of time. SQL Server installation out of the box includes so called BIDS (Business Intelligence Development Studio), which you can use as an IDE. Open it, start from creating a new SQL-CLR project and go from there. If you already have the the full blown version of Visual Studio then the CLR project template is not going to be in BIDS, but will be under Database Projects folder in VS. If you cannot find BIDS on the box where SQL Server is installed then you can re-run the SQL Server installation from the disk and opt to include the components. This will install BIDS.
Jun 20 '12 at 03:58 PM
Oleg
(comments are locked)
|
|
I agree with Pavel with a caveat. If you need to do this inside the database engine then CLR is by far the best way to go, and his post that he linked is an excellent reference. Phil Factor also has a good post on this topic at: CLR Assembly RegEx Functions for SQL Server by Example However, I think you would generally be better off doing it outside of the databse, in the application or presentation layers. You can use SQL's limited pattern matching with If you truly must do this in T-SQL, you certainly can, but I can't think of a way to do it in one query. I would use an initial select with a
(comments are locked)
|
|
Just for fun (and yes just because @TimothyAWiseman said he couldn't think of a way of doing this in one query) here's a single query, pure t-sql solution. Not saying it will be fast though! [please note the code formatter is currently encoding '>' as Ok, I'm impressed.
Jun 22 '12 at 03:48 PM
TimothyAWiseman
Ha Ha - Thank you @TimothyAWiseman!
Jun 22 '12 at 04:00 PM
Kev Riley ♦♦
(comments are locked)
|
|
Thanks Pavan, Oleg, TimothyAwiseman for suggestion. I am planning to use loop to check if there is multiple occurance of my patter.
(comments are locked)
|


Aghh...Cant format it correctly :(
@munabhai Can you please post the desired output as well for the test data? This would help others to better understand your requirement. Thanks.