Please help me create a regular expression to parse my SQL statement - sql

I want to extract
FROM codes WHERE FieldName='ContactMethod' and IsNull(Deactived,'') != 'T'
from
SELECT FieldDescription,FieldValue FROM codes WHERE FieldName='ContactMethod'
and IsNull(Deactived,'') != 'T' order by fielddescription
using a regular expression. I have a regex like this:
\FROM.*\order
which extracts
FROM codes WHERE FieldName='ContactMethod' and IsNull(Deactived,'') != 'T' order
Also, how can I get rid of the capitalization?
Thanks

The trick here would probably be to capture the part you actually want with parens:
(FROM.*) order
This would greedily match until the last order, if you want only until the first occurrence, match lazily:
(FROM.*?) order

Expanding on Fabian Steeg's answer
Dim regex As Regex = New Regex( _
"(FROM.*?) ORDER", _
RegexOptions.IgnoreCase _
Or RegexOptions.CultureInvariant _
Or RegexOptions.IgnorePatternWhitespace _
Or RegexOptions.Compiled _
)
Dim ms As MatchCollection = regex.Matches(InputText)
where InputText is of course your SQL query string.
ms(1) should hold the parentheses match

If it comes down to it you can ignore capitalization by doing (F|f)(R|r)(O|o)(M|m).

Interactive tools like RegexBuddy ($40) or The Regex Coach (free) would really help you to design and debug regular expressions for most platforms.

Related

Oracle SQL constraint to ensure data format

I'm trying to ensure that the data that goes into a cell/field in a data row follows the format YYYYAB, YYYYAC, YYYYAD. Meaning that any four number year and then the two characters "AB" or "AC" or "AD" are valid, anything else would be rejected.
Not sure how to compose the constraint with a like condition and the "_" or the "%" wildcards in order to accomplish this when I'm creating the column.
I was hoping to use something with a syntax like:
constraint cksemester check (SEMESTER in ( _ _ _ _ A B, _ _ _ _ A C, _ _ _ _ A D)),
or a combination of % and Regex..... is there a way to restrict the format to essentially any four numbers
and then force the suffix to be any of "AB" or "AC" or "AD" ?
Thank you.
You can do it without regular expressions using the TRANSLATE function:
CONSTRAINT cksemester CHECK (
TRANSLATE(
semester,
'0123456789',
'0000000000'
) IN ('0000AB', '0000AC', '0000AD')
)
If you want to use regular expressions (which typically execute slower than simple string functions, such as TRANSLATE) then you can use:
CONSTRAINT cksemester CHECK ( REGEXP_LIKE(semester, '^\d{4}A[BCD]$') )
You can use a REGEX to validate your field. A check constraint works like a where condition, so:
ALTER TABLE yourTable
add constraint cksemester
check (REGEXP_LIKE(SEMESTER,'[[:digit:]]{4}(AB|AC|AD)','I'));
This regex ensures:
[[:digit:]]{4} : Exact four digits
(AB|AC|AD) : either AB or AC or AD
the 'I' parameter stands for case insensitive
Edit:
As suggested by MT0 in the comments, a better version of the above regex is ^[[:digit:]]{4}(AB|AC|AD)$
^ ensures the string needs to start there, from the 4 digits
$ ensures the string finishes there, it need to end after the "or" expression.
This improved regex, prevent "outbounds" of the original one such as XYZ1234ABCDEF which would be considered valid and it is not.

How do I extract the "pattern" word when using Like operator?

Hi i'm trying to figure out a way to retrieve a word from the like operator.
Ex:
text = "jsoihj a125847 asf"
Dim s as String = text Like "*a######*"
I would like 's' to equal the actual word that has the pattern of " * a###### * " instead of it returning True
As stated in the above comments Like will not give you the string. You could parse the string character by character to find the pattern but this is a natural job for Regex. I am no expert here so I use sites like RegExr to hack out and test the match string.
dim s as string = Regex.Match(text, "([A][0-9])\w+").Value

Regexp - Pattern matching for multiple characters

I need to pull upto a matching string with below combination: string starting with originMode till URBAN98D....F0F" from the string: version":"7.1.1","originMode":"URBAN98DC66F9-E141-408C-B6A5-99C727571F0F","ModeVersion":
I used below regex:
regexp_extract(string_content ,'^.*originMode\"\:\"(URBAN+)\"',0 )
I am able to pull till URBAN in case if I use the expression as:
regexp_extract(string_content ,'^.*originMode\"\:\"URBAN',0 )
Any help is highly appreciated.
You need to use a negated character class.
regexp_extract(string_content ,'^.*\boriginMode\"\:\"(URBAN[^\"]*)\"',0 )

Convert string using regex expression

I convert it using substring and it working fine but I have to convert lots of the and it will take time.
I was told regex is much more efficient and faster.
Any advice on regex ?
converting string1 to string2 using regex
string1 = '96457fa012456c41bf9200011da2d8fa'
string2='\96\45\7f\a0\12\45\6c\41\bf\92\00\01\1d\a2\d8\fa'
Thank you in advance
This works in Oracle - replacing Oracle's regex implementation with SQL Server's should be straightforward:
select regexp_replace(
'96457fa012456c41bf9200011da2d8fa',
'(..)',
'\\\1')
from dual
Explanation:
we want to match any pair of characters => ".."
we want to "store" the characters we just matched, therefore enclose them in a capturing group => "(..)"
in our replacement string, we want to get the contents of our matching group => "\1"
and we want to add a backslash before each group => "\\\1"
dual is just a dummy table in Oracle

Search for “whole word match” with SQL Server LIKE pattern

Does anyone have a LIKE pattern that matches whole words only?
It needs to account for spaces, punctuation, and start/end of string as word boundaries.
I am not using SQL Full Text Search as that is not available. I don't think it would be necessary for a simple keyword search when LIKE should be able to do the trick. However if anyone has tested performance of Full Text Search against LIKE patterns, I would be interested to hear.
Edit:
I got it to this stage, but it does not match start/end of string as a word boundary.
where DealTitle like '%[^a-zA-Z]pit[^a-zA-Z]%'
I want this to match "pit" but not "spit" in a sentence or as a single word.
E.g. DealTitle might contain "a pit of despair" or "pit your wits" or "a pit" or "a pit." or "pit!" or just "pit".
Full text indexes is the answer.
The poor cousin alternative is
'.' + column + '.' LIKE '%[^a-z]pit[^a-z]%'
FYI unless you are using _CS collation, there is no need for a-zA-Z
you can just use below condition for whitespace delimiters:
(' '+YOUR_FIELD_NAME+' ') like '% doc %'
it works faster and better than other solutions. so in your case it works fine with "a pit of despair" or "pit your wits" or "a pit" or "a pit." or just "pit", but not works for "pit!".
I think the recommended patterns exclude words with do not have any character at the beginning or at the end. I would use the following additional criteria.
where DealTitle like '%[^a-z]pit[^a-z]%' OR
DealTitle like 'pit[^a-z]%' OR
DealTitle like '%[^a-z]pit'
I hope it helps you guys!
Surround your string with spaces and create a test column like this:
SELECT t.DealTitle
FROM yourtable t
CROSS APPLY (SELECT testDeal = ' ' + ISNULL(t.DealTitle,'') + ' ') fx1
WHERE fx1.testDeal LIKE '%[^a-z]pit[^a-z]%'
If you can use regexp operator in your SQL query..
For finding any combination of spaces, punctuation and start/end of string as word boundaries:
where DealTitle regexp '(^|[[:punct:]]|[[:space:]])pit([[:space:]]|[[:punct:]]|$)'
Another simple alternative:
WHERE DealTitle like '%[^a-z]pit[^a-z]%' OR
DealTitle like '[^a-z]pit[^a-z]%' OR
DealTitle like '%[^a-z]pit[^a-z]'
This is a good topic and I want to complement this to someone how needs to find some word in some string passing this as element of a query.
SELECT
ST.WORD, ND.TEXT_STRING
FROM
[ST_TABLE] ST
LEFT JOIN
[ND_TABLE] ND ON ND.TEXT_STRING LIKE '%[^a-z]' + ST.WORD + '[^a-z]%'
WHERE
ST.WORD = 'STACK_OVERFLOW' -- OPTIONAL
With this you can list all the incidences of the ST.WORD in the ND.TEXT_STRING and you can use the WHERE clausule to filter this using some word.
You could search for the entire string in SQL:
select * from YourTable where col1 like '%TheWord%'
Then you could filter the returned rows client site, adding the extra condition that it must be a whole word. For example, if it matches the regex:
\bTheWord\b
Another option is to use a CLR function, available in SQL Server 2005 and higher. That would allow you to search for the regex server-side. This MSDN artcile has the details of how to set up a dbo.RegexMatch function.
Try using charindex to find the match:
Select *
from table
where charindex( 'Whole word to be searched', columnname) > 0