SQL find all rows that do not have certain characters - sql

I want to find all rows for which values in string column does not possess certain characters (to be specific [a-Z0-9 \t\n]) how can I do it in sql ?
I tried to do it with like operator
SELECT ***
where column like '%[^ a-Z0-9 \t\n]%'
however, it does not work and I get rows that possess characters and numbers.

To fetch all records that contain any characters other than alphabets, numbers, spaces, tabs and new-line delimiters:
SELECT ***
WHERE column like '%[^A-Za-z0-9 \t\n]%'
Note that [^A-Za-z0-9 \t\n] represents anything other than alphanumeric characters, spaces, tabs, and new line delimitters.

Your logic is inverted. I think you want:
where column not like '%[^ a-Z0-9 \t\n]%'
I don't think that SQL Server interprets \t and \n as special characters. You may need to insert the actual values for the characters. (See here.)

SELECT ***
WHERE column like '%[^A-Za-z0-9 \t\n]%'

Related

Select column ignore beginning numbers

I have a column that I need to select but it has an inconsistent amount of numbers/formatting in the beginning
The column values are ideally supposed to be structured like:
# Question_-_Answer
But here are some examples which make it hard to remove the numbers in the beginning
0 Question1_-_50-60
1.Question_-_apple
12Question_-_40/50
13 Question_-_orange
14.Question_-_apple
15. Question_-_orange2
Is there a way I can query this column so that it ignores everything until the first alphabetical character while also not removing any characters/alphanumerical values in the question and answer portion?
You can use PATINDEX and STUFF to achieve this:
SELECT STUFF(V.YourString,1,PATINDEX('%[A-z]%',V.YourString)-1,'')
FROM (VALUES('0 Question1_-_50-60'),
('1.Question_-_apple'),
('12Question_-_40/50'),
('13 Question_-_orange'),
('14.Question_-_apple'),
('15. Question_-_orange2'))V(YourString);
This removes all characters up to the first alpha character.

Getting the Column containing the non-english language in ORACLE

I have above entries in my database, my requirement is to extract the fields containing the non-english language characters ( including if the data containing the combination of english and non-english characters like HotelName field for the ID 45).
I tried by regexp_like function by looking for the alphanumeric and non-alphanumeric, but i have some data with combination of both the condition fails there.
Thanks in Advance
Raghavan
Does this do what you want?
where regexp_like(hotelname, '[^a-zA-Z0-9 ]')
That is, where the hotel name contains any character that is not a "letter" or digit. You may need to take additional characters into account as well, such as commas, periods, and hyphens.

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

Display certain sequence only in VARCHAR

I have a column error_desc with values like:
Failure occurred in (Class::Method) xxxxCalcModule::endCustomer. Fan id 111232 is not Effective or not present in BL9_XXXXX for date 20160XXX.
What SQL query can I use to display only the number 111232 from that column? The number is placed at 66th position in VARCHAR column and ends 71st.
SELECT substr(ERROR_DESC,66,6) as ABC FROM bl1_cycle_errors where error_desc like '%FAN%'
This solution uses regular expressions.
The challenge I faced was on pulling out alphanumerics. We have to retain only numbers and filter out string,alphanumerics or punctuations in this case, to detect the standalone number.
Pure strings and words not containing numbers can be easily filtered out using
[^[:digit:]]
Possible combinations of alphanumerics are :
1.Begins with a character, contains numbers, may end with characters or punctuations :
[a-zA-Z]+[0-9]+[[:punct:]]*[a-zA-Z]*[[:punct:]]*
2.Begins with numbers and then contains alphabets,may contain punctuations :
[0-9]+[[:punct:]]*[a-zA-Z]+[[:punct:]]*
Begins with numbers then contains punctuations,may contain alphabets :
-- [0-9]+[a-zA-Z][[:punct:]]+[a-zA-Z] --Not able to highlight as code, refer solution's last regex combination
Combining these regular expressions using | operator we get:
select trim(REGEXP_REPLACE(error_desc,'[^[:digit:]]|[a-zA-Z]+[0-9]+[[:punct:]]*[a-zA-Z]*[[:punct:]]*|[0-9]+[[:punct:]]*[a-zA-Z]+[[:punct:]]*|[0-9]+[a-zA-Z]*[[:punct:]]+[a-zA-Z]*',' '))
from error_table;
Will work in most cases.