How do I extract the "pattern" word when using Like operator? - vb.net

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

Related

String Regex Expressions in Redshift SQL

I am pretty new to using string operations in SQL(Redshift). I want to extract a part of a string from the strings of the following format:
I can have strings of the format:
http://bunnytalks.com/goingOn?name=Bunny&phone=2340
http://bunnytalks.com/goingOn?name=Bunny
http://bunnytalks.com/goingOn?name=Talks/whatson%goingOn%name%Bunny
http://bunnytalks.com/goingOn?name=Talks/whatson%goingOn%name%Bunny&phone=2340
The final output I need from any of the above strings when applying regex:
Bunny
From the above string examples, I can tell that I need a string between the last occurrence of a name followed by either = or % and the end of the string or before &
I need a regex/ any string operations in SQL that can achieve the above operations as shown in examples. Thanks in advance.
Try:
.*name[%=]([^&\n]+)
Regex demo.
.*name - match the last name
[%=] - followed by % or =
([^&\n]+) - match all non-&, non-\n characters as group 1

Check if a string has a combination of a substring and numbers in sql

how do I write a SQL where statement that checks if a string contains some substring and a number. For example:
string: macsea01
where string like 'macsea' plus a number
Regex is the most obvious solution to this question. Without more detail about the specific format of the string, I can suggest the following, which will match a sequence of a letter in the alphabet followed immediately by a digit:
where column_name like '%[a-zA-Z][0-9]%'
If you're literally looking for macsea at the beginning of the string followed by a digit, it would be:
where column_name like 'macsea[0-9]%'
Regex seem to bee a little slippery here, depending on your needs you can for instance divide the string into several parts, first the text part, and take the rest of the string, try to convert it into a number.
Somthing like this (but I think this perticular code is broken
where substring(column_name, 1, 6) = 'macsea' and cast(substring(column_name, 7, 1000) as int) > 0

VB.NET How to check if a string follows a format

what i'm looking for is a way for a way to check if a string follows a specific format.
Something Like:
if strPhoneNumber = format(XX-XXXX-XXXX) then
MsgBox("Correct")
else
MsgBox("Incorrect")
the specific format is Two Numbers, a Dash, Four Number, A Dash, Four Numbers: eg: 04-9567-3915
Thanks.
You want to parse a Regular Grammar, so use a regular expression:
Dim regex As New Regex( "\d\d\-\d\d\d\d\-\d\d\d\d" );
If regex.IsMatch( strPhoneNumber ) Then
MsgBox("correct")
End If

How to compare Text Datatype in SQL

I have a column named filePath, its datatype is TEXT. How can I compare a filePath with another String (case sensitive)?
Example:
SELECT *
FROM source_code_links
WHERE filePath =
"C:\CtrlSpaceData\CtrlSpace VCS\ba4fe9cbcbdb14a7cbc91fdf65f9e98178afe353\src\servlets\Test.java";
I have tried LIKE, but it doesnt work as well.
I've also tried PATINDEX but there as error saying function PATINDEX does not exists.
These are the things I've tried:
SELECT * FROM source_code_links
WHERE filePath LIKE "%ba4fe9cbcbdb14a7cbc91fdf65f9e98178afe353\src\servlets\Test.java";
SELECT * FROM source_code_links
WHERE filePath LIKE "C:\CtrlSpaceData\CtrlSpace VCS\ba4fe9cbcbdb14a7cbc91fdf65f9e98178afe353\src\servlets\Test.java";
SELECT * FROM source_code_links
WHERE PATINDEX("C:\CtrlSpaceData\CtrlSpace VCS\ba4fe9cbcbdb14a7cbc91fdf65f9e98178afe353\src\servlets\Test.java", filePath) > 0;
Are there other solutions available??
you need to use single not double quotations
You can find out if it equals to other string by using = operator or if it's unequal to other string by using <> operator. Other options may include using like function you've mentioned, but then you need to take some part of the other string by some algorithm and then append % symbol on place where you removed characters.

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