How to using regexp_contains for the similiar first word - sql

i have a data with entrance_page_name:
/search?q=
/search?
/search?ast
can i get the data with the similar first word
WHEN REGEXP_CONTAINS(entrance_page_name, '^/search/q=') THEN 'search?q='
WHEN REGEXP_CONTAINS(entrance_page_name, '^search?') THEN 'search?'
But it's not really works. Any assistance with this would be greatly appreciated!
Thank you

You can possibly use the raw string, and an escape character to override ? symbol.
SELECT CASE WHEN REGEXP_CONTAINS('/search?q=SDbmoLZK89s', r'^/search\?q=') THEN 'search?q=' END as test
The above code should ideally work in your situation.

Related

SQL:How to find similar strings in a tuple

I tried to use difflib to get_close_matches in a tuple data...but it does not work...I have earlier used difflib in a JSON file but couldn't use it in an SQL...Result expectationI want to find words similar to the input given..even if there is any spelling mistake...for example...if the input is treeeee or TREEEEE or Treeea...my program should consider the nearest match...that is a tree...Similar to the Did you mean? function in GOOGLE. I also tried SELECT * FROM Dictionary WHERE Expression LIKE '%s but the problem persists. Please help me solve this. Thanks in advance.
SQL functions Soundex and DIFFERENCE look like the closest fit.

Not Like in Teradata

I am new to Teradata and trying to figure out how to do a NOT LIKE statement with multiple wildcards. I've tried several different ways, but haven't found a way that works. Most recently I've tried the code below.
WHERE DIAG_CD NOT IN ALL ('S060%','S340%')
Any help you all can provide would be much appreciated.
Thanks!
You are on the right track. You can use ANY / ALL quantifier with LIKE or NOT LIKE.
WHERE DIAG_CD NOT LIKE ALL ('S060%','S340%')
or
WHERE NOT (DIAG_CD LIKE ANY ('S060%','S340%'))
IN does not support wildcards. You need to repeat the conditions:
where diag_cd not like 'S060%' and diag_cd not like 'S340%'
Or you can do regex matching instead: ^ represents the beginning of the string, and | stands for or. This syntax is easier to extend with more strings patterns.
where not regexp_like(diag_cd, '(^S060)|(^S340)')

Substring with delimiter in SQL

I'm trying to return a substring of the following, it's comma delimited [only one comma]
City-City-City, State-State-State
Sometimes it's only one city and state, sometimes it's more than one of either [or both]
Basically, I need to just return the state initials pass the comma.
What's the best way to do this? I'm looking into the substring function, but that doesn't seem that smart. I found a split function but it looks like overkill and I don't like to use code I don't understand.
Ex:
Cincinnati-Middletown, OH-KY-IN
Cleveland-Elyria-Mentor, OH
Abilene, TX
Output:
OH-KY-IN
OH
TX
Thanks for the answers;I just figured it out thanks to Sonam's starting point.
Here's what I got. Haven't looked into it but it seems to returning the right stuff.
select substring(CBSAName,charindex(',',CBSAName)+1, LEN(CBSAName)) FROM CBSAMasterList
select substring('Abilene, TX',charindex(',','Abilene, TX')+2,2)

Regex to replace asterisk characters with html bold tag

Does anyone have a good regex to do this? For example:
This is *an* example
should become
This is <b>an</b> example
I need to run this in Objective C, but I can probably work that bit out on my own. It's the regex that's giving me trouble (so rusty...). Here's what I have so far:
s/\*([0-9a-zA-Z ])\*/<b>$1<\/b>/g
But it doesn't seem to be working. Any ideas? Thanks :)
EDIT: Thanks for the answer :) If anyone is wondering what this looks like in Objective-C, using RegexKitLite:
NSString *textWithBoldTags = [inputText stringByReplacingOccurrencesOfRegex:#"\\*([0-9a-zA-Z ]+?)\\*" withString:#"<b>$1<\\/b>"];
EDIT AGAIN: Actually, to encompass more characters for bolding I changed it to this:
NSString *textWithBoldTags = [inputText stringByReplacingOccurrencesOfRegex:#"\\*([^\\*]+?)\\*" withString:#"<b>$1<\\/b>"];
Why don't you just do \*([^*]+)\* and replace it with <b>$1</b> ?
You're only matching one character between the *s. Try this:
s/\*([0-9a-zA-Z ]*?)\*/<b>$1<\/b>/g
or to ensure there's at least one character between the *s:
s/\*([0-9a-zA-Z ]+?)\*/<b>$1<\/b>/g
I wrote a slightly more complex version that ensures the asterisk is always at the boundary so it ignores hanging star characters:
/\*([^\s][^\*]+?[^\s])\*/
Test phrases with which it works and doesn't:
This one regexp works for me (JavaScript)
x.match(/\B\*[^*]+\*\B/g)

MySQL - Where - search string - MATCH

Quick question. I'm in a bit of a rush but if someone could quickly point me in the right direction I would be very very happy.
I have a field in the db, let's call it field_a which returns a string in the format "20,50,60,80" etc.
I wish to do a query which will search in this field to see if 20 exists.
Could I use MySQL MATCH or is there a better way?
Thank you!
The better way would be to save the data differently. With WHERE a LIKE '...' and MATCH/AGAINST (besides being fairly slow) you can't easily search for just "20"... If you search for "20" you'll get "200" too; if you search for ",20," you won't get "20, 50"
Use FIND_IN_SET:
WHERE FIND_IN_SET(20, field_a) != 0
Just be careful you don't get a substring of what you actually want - for example LIKE '%20%' would also match '50,120,70'. If you're using MySQL, you might want to use REGEXP '[[:<:]]20[[:>:]]' - where the funny faces are word boundary markers that will respect break on beginning / end of string or commas so you shouldn't get any false positives.
http://dev.mysql.com/doc/refman/5.1/en/regexp.html