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

string extraction help Expand / Collapse
Author
Message
Posted Monday, January 14, 2013 9:06 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 22, 2013 8:51 AM
Points: 123, Visits: 228
Hi,

I have some code in c# which extracts the text from a string that complies with these wildcards

([A-Za-z]{3,6}_?[0-9]{4,7})

to break this down
first 3 to 6 characters needs to be upper or lower case letters
then there could be an underscore
then the next 4 to 7 characters have to be numbers

Does anyone know how to create a sql equivalent. Should I use PATINDEX, if so how to I denote the max and min length?

Many Thanks for your help, it is very much appreciated,

Oliver
Post #1406789
Posted Monday, January 14, 2013 9:21 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 10:26 PM
Points: 8,606, Visits: 8,247
Can you use CLR? RegEx tends to be far easier to implement in C# than in pure t-sql. There are certainly some pattern matching techniques for some basics but I am unaware of more complicated matching like you have going on.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1406802
Posted Monday, January 14, 2013 9:33 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 22, 2013 8:51 AM
Points: 123, Visits: 228
Thanks for this, my CLR skills are non - existent but it is something I need to look into. I don't think such powerful query tools exist to do this in SQL, I guess.

Many Thanks for the reply.
Post #1406811
Posted Monday, January 14, 2013 10:37 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:43 AM
Points: 4,242, Visits: 9,491
oliver.morris (1/14/2013)
Thanks for this, my CLR skills are non - existent but it is something I need to look into. I don't think such powerful query tools exist to do this in SQL, I guess.

Many Thanks for the reply.


No they don't - this requirement is definitely a strong CLR candidate.


____________________________________________________________________________________________

Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:

http://www.sqlservercentral.com/articles/Best+Practices/61537/

If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
Post #1406846
Posted Monday, January 14, 2013 10:41 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:43 AM
Points: 4,242, Visits: 9,491
Or if you can use SSIS to achieve whatever it is that you are doing, there is at least one CodePlex add-on that would allow you to use RegEx matching without having to write code ...

____________________________________________________________________________________________

Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:

http://www.sqlservercentral.com/articles/Best+Practices/61537/

If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
Post #1406850
Posted Monday, January 14, 2013 10:45 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 22, 2013 8:51 AM
Points: 123, Visits: 228
Many Thanks for your help, this is good to know. Will try the CLR route.

Cheers
Post #1406853
Posted Monday, January 14, 2013 5:39 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:39 AM
Points: 2,345, Visits: 3,189
It might seem a bit tricky and you may need to play with this a bit to get it working 100%, but I think you'll get the idea of what I'm trying to do here pretty quickly.

WITH SampleData (s) AS (
-- RegEx: ([A-Za-z]{3,6}_?[0-9]{4,7})
SELECT 'ABVAAA_1234567' -- valid
UNION ALL SELECT 'AASDAAA_123' -- invalid
UNION ALL SELECT 'ACd123' -- valid
UNION ALL SELECT 'ACD_12345677' -- invalid
UNION ALL SELECT '12345677' -- invalid
)
SELECT s, PosUnd, PosNum, PosAlph, LEN(s)
,IsValid=CASE
WHEN PosUnd > 0 AND PosAlph <> 0 AND PosNum - PosUnd = 1 AND PosUnd - PosAlph <= 6 AND LEN(s) - PosNum <= 6
THEN 1
WHEN PosUnd = 0 AND PosAlph <> 0 AND PosUnd - PosAlph <= 7 AND LEN(s) - PosNum <= 7
THEN 1
--WHEN
ELSE 0 END
FROM SampleData
CROSS APPLY (SELECT PATINDEX('%[_]%', s)) a(PosUnd)
CROSS APPLY (SELECT PATINDEX('%[0-9]%', s)) b(PosNum)
CROSS APPLY (SELECT PATINDEX('%[A-Za-z]%', s)) c(PosAlph)


Alternatively, using CLRs and understanding how to write them don't need to go hand in hand. You just need to have permissions on your database to install them.

Here's a pretty nice library called SQL# developed by Solomon Rutzky (SQL Sharp Library of CLRs) that is really clean to install, is well documented and has the functions in it that you'll need to perform this validity check directly with the RegEx pattern you've provided. RegEx validations will probably be more costly in terms of CPU than doing something like what I provided, but you can always test that assumption (and you should) to be sure.



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1406994
Posted Tuesday, January 15, 2013 2:02 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 22, 2013 8:51 AM
Points: 123, Visits: 228
I see what you are up to here. This is a really neat method and will work it up. Thank you for sending over the CLR reference, I will check this out as well.

I really appreciate your help,

Oliver
Post #1407090
Posted Tuesday, January 15, 2013 2:09 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:39 AM
Points: 2,345, Visits: 3,189
oliver.morris (1/15/2013)
I see what you are up to here. This is a really neat method and will work it up. Thank you for sending over the CLR reference, I will check this out as well.

I really appreciate your help,

Oliver


Happy to be of service!



No loops! No CURSORs! No RBAR! Hoo-uh!

INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1407097
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse