What will be the regular expression for alphanumeric characters, space ,french characters and dash? - sql

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

Related

PLSQL - help on my regex phone number with area code

I have difficulties to find the right regex under PL/SQL, but my regex is normally good
I have a phone number like this :
+44 (0)22 3333 4444 from the text that should not be there
And I want to get this:
+4402233334444
So I made the following regex:
/[^+\d]|\s/g
It works very well on the site https://regexr.com/ but not in my PL/SQL query, it gives me the same result
I tried to use the oracle doc, but without success
https://www.techonthenet.com/oracle/regexp_like.php
The \d and other shorthand character classes should not be used inside a bracket expression.
You can use
SELECT
REGEXP_REPLACE(
'+44 (0)22 3333 4444',
'[^+0-9]',
''
) As Result FROM dual;
where [^+0-9] matches any char other than + and a digit.
See the DB fiddle.
Note that [^+0-9] already matches any whitespace chars since non-digit chars other than + also match what \s matches, so you can safely omit the |\s from your regex.

How to find a row where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL

I need to find rows where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL.
I am doing like below:
SELECT *
FROM test
WHERE Name_test LIKE '%[^A-Za-z _]%'
But It is not working and I also need to exclude any apostrophe.
Kindly help.
If you need to find all rows where column have ONLY numbers and special characters (and you can specify all of required special characters):
SELECT *
FROM test
WHERE regexp_like(Name_test, q['^[0-9'%##]+$]')
as you can see you just need to add your special characters after 0-9.
^ - start
$ - end
About format q'[SOMETHING]' please see TEXT LITERALS here: https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Literals.html#GUID-1824CBAA-6E16-4921-B2A6-112FB02248DA
If you need to find all rows where column have no alpha-characters:
SELECT *
FROM test
WHERE regexp_like(Name_test, '^[^a-zA-Z]*$');
or
SELECT *
FROM test
WHERE regexp_like(Name_test, '^\W*$');
about \W - please see "Table 8-5 PERL-Influenced Operators in Oracle SQL Regular Expressions" here:
https://docs.oracle.com/database/121/ADFNS/adfns_regexp.htm#ADFNS235
I need to find rows where col have special characters or numbers (except hyphen, apostrophe and space [and presumably single quotes]) in Oracle SQL.
You can use double single quotes to put a single quote in:
WHERE Name_test LIKE '%[^-A-Za-z _'']%'
However, this is not Oracle syntax. If the above works, then I would guess you are using SQL Server. In Oracle:
WHERE REGEXP_LIKE(Name_test, '[^A-Za-z _'']')

Find phone numbers with unexpected characters using SQL in Oracle?

I need to find rows where the phone number field contains unexpected characters.
Most of the values in this field look like:
123456-7890
This is expected. However, we are also seeing character values in this field such as * and #.
I want to find all rows where these unexpected character values exist.
Expected:
Numbers are expected
Hyphen with numbers is expected (hyphen alone is not)
NULL is expected
Empty is expected
Tried this:
WHERE phone_num is not like ' %[0-9,-,' ' ]%
Still getting rows where phone has numbers.
from https://regexr.com/3c53v address you can edit regex to match your needs.
I am going to use example regex for this purpose
select * from Table1
Where NOT REGEXP_LIKE(PhoneNumberColumn, '^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$')
You can use translate()
...
WHERE translate(Phone_Number,'a1234567890-', 'a') is NOT NULL
This will strip out all valid characters leaving behind the invalid ones. If all the characters are valid, the result would be NULL. This does not validate the format, for that you'd need to use REGEXP_LIKE or something similar.
You can use regexp_like().
...
WHERE regexp_like(phone_num, '[^ 0123456789-]|^-|-$')
[^ 0123456789-] matches any character that is not a space nor a digit nor a hyphen. ^- matches a hyphen at the beginning and -$ on the end of the string. The pipes are "ors" i.e. a|b matches if pattern a matches of if pattern b matches.
Oracle has REGEXP_LIKE for regex compares:
WHERE REGEXP_LIKE(phone_num,'[^0-9''\-]')
If you're unfamiliar with regular expressions, there are plenty of good sites to help you build them. I like this one

Regex not matching correct string

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.

Postgresql search using only alphanumeric characters

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'