It's pretty straightforward to strip out non-alphanumeric characters out from the search term, but how do you compare it to only the non-alphanumeric characters of values in the database?
For example, if I search for stack's, how can I get it to match both stacks and stack's?
What do I need to do to the what-do-i-do variable below to make the above happen?
SELECT * FROM table WHERE <what-do-i-do> ilike 'stacks'
One way to do this is with translate:
select *
from table
where translate(lower(WhatIDo), translate(lower(WhatIDo), 'abcdefghijklmnopqrstuvwxyz', ''), '') = 'stacks'
The inner translate finds all non-alpha characters. The outer then removes these from the string.
You can try to replace all instances of non-alphanumeric characters with the wildcard character ('%'). In your example:
SELECT * FROM table WHERE data like 'stack%s'
Related
I am busy building a lookup table for specific names of merchants. I tried to make use of the following regex but it's returning less results than the standard "like" function in Netezza SQL. Please refer to below:
SQL Like function: where trim(upper(a.MRCH_NME)) like '%CNA %' -- returns 4622 matches
Regex function in Netezza SQL: where array_combine(regexp_extract_all(trim(upper(a.MRCH_NME)),'.*CNA\s','i'),'|') = 'CNA' -- returns 2226 matches
I looked at the two result sets and found that strings such as the following aren't matched:
!C CNA INT ARR
*CNA PLATZ 0400
015764 CNA CRAD
C#CNA PARK 0
I made use of the following regex expression: /.*CNA\s'/
Any idea why the above strings aren't being returned as matches?
Thank you.
You probably should be using regexp_like:
SELECT *
FROM yourTable
WHERE REGEXP_LIKE(MRCH_NME, 'CNA[ ]', 'i');
This would be logically identical to the following query using LIKE:
SELECT *
FROM yourTable
WHERE MRCH_NME LIKE '%CNA ';
It seems to me the problem is more with your code rather than the regex. Look: like '%CNA %' returns all entries that contain a CNA substring followed with a literal space anywhere inside the entry. The '.*CNA\s' regex matches any 0+ chars other than newline followed with CNA and **any whitespace char*.
Acc. to this reference, \s matches "a white space character. White space is defined as [\t\n\f\r\p{Z}].
Thus, you should in fact just use
WHERE REGEXP_LIKE(MRCH_NME, 'CNA ', 'i')
or, better with a word boundary check:
WHERE REGEXP_LIKE(MRCH_NME, '\bCNA\b', 'i')
where \b marks a transition from a word to non-word and non-word to word character, thus ensuring a whole word search and justifying the regex usage.
If you do not need to match the merchant name as a whole word, use the regular LIKE with '%CNA %', it should be more efficient.
I just want to know what will be the regex for alphanumeric characters, space french characters and dash. I tried this, but it doesn't work.
SELECT * FROM my_table
WHERE regexp_like(name_elem1,'[^[:alnum:]^[:blank:]^[àâçéèêëîïôûùüÿñæœ]^[\-]]');
Please help
I am not an Oracle SQL expert and cannot test the solution but I would rather write it the following way:
SELECT * FROM my_table WHERE regexp_like(name_elem1,'[0-9A-Za-z\ \tàâçéèêëîïôûùüÿñæœ]+');
Different sources say that one cannot join regex character classes so I have put them explicitly: [0-9A-Za-z] for alnum, \ \t for white characters and an extended list of French characters.
If you want those characters, then don't use the 'not' expression...and consider case-insensitivity.
... regexp_like(name_elem1,'[[:alnum:][:blank:][àâçéèêëîïôûùüÿñæœ]]', 'i');
caveot: this is just looking for any 1 character matching the expression.
Here's the official doc:
https://docs.oracle.com/database/121/SQLRF/ap_posix.htm#SQLRF020
Text = “World“ world ” <world <Word> ‘ word’ ‘ word“ word’ =1254.25 = 2545.58. 20%
Hey guys,
I need to search a word from a string which was saved in my db. The searching word contains special characters such as ""'!##$%^<>. Below is the sql select query used for the search.
select * from TABLE where TABLE.text like ('%'+ '“ World' +'%')
as a result some of the special character are not searched. Characters such as double quotation, single quotation are not searched from this. need assistance with solving this problem asap. thank you :)
These Special characters are Unicode characters, When ever dealing with these characters in sql server you have to tell sql server explicitly that there can be some unicode characters in the strings you are about to manipulate by prefixing your strings with N'String'
In your case you would write a query something like ....
select * from TABLE
where TABLE.text like N'%'+ N'“ World' + N'%'
you can use the ESCAPE clause and escape your parameter value. http://technet.microsoft.com/en-us/library/ms179859.aspx
so for example to search for a string containing the character %
select * from TABLE where TABLE.text like ('%'+ N'\%' +'%') ESCAPE '\'
I am using this query to replace one character in a cell
select replace(id,',','')id from table
But I want to replace two characters in a cell.
If the cell is having this data (1,3.1), and I want it to look like this (131).
How can I replace two different characters in one cell?
Use TRANSLATE instead of REPLACE(). It replaces each occurrence of a character in the first pattern with its matched character in the second. To remove characters, simply leave cut short the replacement string:
select translate(id, '1,.', '1') id from table
Note that the second string cannot be null. Hence the need to include 1 (or some other character) in both strings.
Find out more.
Obviously the more characters you need to convert/remove the more attractive TRANSLATE() becomes. The main use for REPLACE is changing patterns (such as words) rather than individual characters.
Can use
select replace(translate(id,',.',' '),' ','') from table;
or
select regexp_replace('1,3.1','[,.]','') from dual;
or
select replace(replace(id,',',''),'.','') from table;
Call the replace again.
select replace(replace(id,',',''), '.','') id from table
Do this:
select REPLACE(REPLACE(id,',',''),'.','')
Or use a regular expression:
select regexp_replace(id, '[.,]', '') id from table
Find out more
I am trying to select data containing four percentage signs in a row. How can I escape the percentage signs so my LIKE condition works?
Thanks
Use #% with the escape character clause:
select *
from tbl
where fld like '%#%%' escape '#'
This will search for all records that contain the "%" character in the fld column.
DB2/z has a slightly different format:
select *
from tbl
where fld like {escape '#'} '%#%%'
Obviously, you'll need to choose your escape character carefully so it won't interfere with the rest of your string but this is relatively easy for static strings. Dynamically built strings will require dynamically built queries so that it doesn't use a character from the string.