SQL like statement "%[^0-9]%" not working - mysql

I have a table.
USER TABLE Columns:
user_name (varchar)
age (int)
address (varchar)
telephone (varchar)
I want to seach for user whose address field contains digits.
I tried the following sql statement in MySQL Workbench:
SELECT * FROM user WHERE address LIKE '%[^0-9]%';
However, it did not work. Can anyone help me out here?
Thanks in advance.

You should remove the ^ character from the brackets:
LIKE '%[0-9]%'
It doesn't want to be a regex, it is the [charlist] wildcard

I believe you're looking for REGEXP:
SELECT *
FROM user
WHERE address REGEXP '.*[^0-9].*'
Example of finding addresses without numbers

select * from user where first_name REGEXP '^[0-9].*'
This gives you all records in your user table where the first name starts with numbers 0-9.

'%[^0-9]%'
Will search for the column that has %[^0-9]% as text
It won't do a Regex on it
Try
'.[^0-9].' // That performs regex

Related

Cannot check if a field contains also number

I'm trying to check if a field in a specific table contains also number, in particular I have a record that have the field name which contains this value: Besëlidhja Lezhë vs. Tërbuni Pukë 1 - 1, so I'm trying to get also all the rows of that table that contains a number inside the field name. I tried:
SELECT * FROM `venue` where `name` like '%[0-9]%'
but this will return an empty result, any idea?
This should tell you if name contains any digit (not tested)
SELECT * FROM venue WHERE name REGEXP '[0-9]'
You can try using a Regular Expression that filters for names in your name column with numeric characters . For example:
SELECT * FROM DATA WHERE name REGEXP '[a-z]...[0-9]';
mySQL allows you to use regular expression as a filter !
This should select out for names like Tërbuni Pukë 1 - 1. If you want to practice regular expressions this is a great website to test whether you have the right regex. https://regex101.com/
Hope this helps !
I believe this should work:
SELECT *
FROM venue
WHERE name like '%0%' or name like '%1%' or name like '%2%' or name like '%3%'
and so on til you get to 9. I hope this helps

Mysql regex search over an array of phone numbers

Need help in executing a query over a customer database on a field named phone.
select * from customers where phone REGEXP '123456|565834'
I need a way to select the matched portion of regex matched in select clause.
The final results should be something like
Name Matched Phone
Naveen 123456 12345678
Naveen2 123456 123456789
Arun 565834 9565834
Arun2 565834 10565834
P.S. This has to be one query and there is no other unique key to be grouped by with
Use INSTR function of MySQL.
Ex. INSTR(regex, phone)
SELECT SUBSTRING('123456|565834',INSTR('123456|565834',phone),10)
FROM customers
WHERE phone REGEXP '123456|565834';
Doc: INSTR function
You may use a CASE expression here:
SELECT
Name,
CASE WHEN Phone REGEXP '123456' THEN '123456'
WHEN Phone REGEXP '565834' THEN '565834' END AS Matched,
Phone
FROM customers
WHERE
phone REGEXP '123456|565834';
Note that you don't necessarily need to use REGEXP in this case, LIKE probably would have sufficed. But using REGEXP is easier to read IMO, and also it lets you handle more complex logic, should you need to in the future.
If you're not constrained to only use mysql, then may try with shell script + mysql to pass the map values and search - Not sure understood the requirement correctly.
search="5672C";
mysql <connection details> -e "select fields, \"$search\" as matched from customers where phone like \"%$search%\";"
We can loop the map strings and achieve.

Mysql search in database

I have name,surname,city,address,mobile fields in mysql table.
I want a select statement that select all the records if any of the above fields data matches;
For eg: If i put name: yusuf. then select statement should look data :yusuf in all column mentioned above and if any of the record matches then it should show the result.
Lets say you search by name.
Then use SELECT * FROM table WHERE name LIKE userinput.
Hope this help
select name,surname,city,address,mobile from Table
where name like '%Yusuf%'
Or Surname like '%Yusuf%'
or City like '%Yusuf%'
Or Address like '%Yusuf%'
Or Mobile like '%Yusuf%'

Mysql query, contains a string then 3 numbers in email

I have a mysql database with a table that contains an email address entered by website users.
How would I select all records where the email field contains any number of characters, then 3 numbers and #yahoo.com
i.e. testemail639#yahoo.com
A simple way would be to use REGEXP in your SELECT statements.
SELECT * FROM records WHERE email REGEXP '^\w+\d{3}\#.*$'
The above statement is untested, but should lead you down a better road.
SELECT * FROM table WHERE email REGEXP '[0-9]{3}#yahoo\.com'
Try this
SELECT * FROM records WHERE email REGEXP '^\w+([\.-]?\w+)?([0-9]{3})#\w+([\.-]?\w+)*(\.\w{2,4})+$'
^\w+([\.-]?\w+)?([0-9]{3})#\w+([\.-]?\w+)*(\.\w{2,4})+$ strictly checks string followed by 3 numbers pattern in the email containing no spaces

mysql select query - to match one field with the other field

I am trying to find records that has the following scenario.
ID | name | email
1 Robert robert#gmail.com
2 William bill#gmail.com
3 Michael michael#gmail.com
4 Micahel mike#gmail.com
Based on the above table, I want to find the records where the "name" is contained in the "email field", here record 1 and 3 should be the output and not 2 and 4. Is there any way I can do this comparison?
I tried reading about regex but couldn't find anything. If it's comparison of same value, it will be straightforward, but I am not having any clue for this one. I thought of LIKE but looks like this cannot have field names.
The exact syntax will depend on how you want to define the relationship.
Are you looking for the name anywhere in the email address? (This will be slow)
select id,name,email
from your_table
where email like concat('%',name,'%')
Just at the beginning of the email address?
select id,name,email
from your_table
where email like concat(name,'%')
Just before the # sign?
select id,name,email
from your_table
where email like concat(name,'#%')
You can use LIKE, you just have to use it in combination with CONCAT.
SELECT
ID,
name,
email
FROM
yourTable
WHERE
email LIKE CONCAT(name, '%');
The CONCAT will return a string which can be used to match against email via LIKE.
This should work
SELECT * FROM table WHERE email LIKE (CONCAT('%',name,'%'))
select * from your_table where lower(substring_index(email,'#',1))=lower(name)