Im looking to select only valid number values from table (ean, gtin, upc or barcode)
What is the difference between the 2 statements, as they give me different results.
select count(*) from catalogue where barcode NOT REGEXP '^[0-9]+$';
select count(*) from catalogue where barcode NOT LIKE '%[0-9]%';
Regards
This logic in MySQL:
where barcode NOT REGEXP '^[0-9]+$';
chooses any barcode that is does not consist only of digits.
This logic in MySQL:
where barcode NOT LIKE '%[0-9]%';
chooses any barcode that does not have the four characters '[',, '0', '-', and ']' adjacent to each other in that order in the string.
In other words, LIKE does not recognize character classes in MySQL.
You can also write the logic to identify non-numbers as:
where barcode REGEXP '[^0-9]'
That is, it contains a character that is not a digit.
Related
I have a table that has nvarchar elements.
This table has two kinds of elements:
elements with only digit characters
elements with digit characters and the 3rd character is non-English character
I want a query to get all rows that their 3rd character is non-English.
EDIT
use WHERE SUBSTRING(<table>.ColumnName, 3, 1) NOT BETWEEN '0' AND '9' worked for me either
I'd use regexp_like with a regex that the third character isn't a digit:
SELECT *
FROM mytable
WHERE REGEXP_LIKE(mycol, '..[^[:digit:]].*')
In MySQL versions older than 8.0, you could use the regexp operator:
SELECT *
FROM mytable
WHERE mycol REGEXP '..[^[:digit:]].*'
You can use RLIKE operator, below is the query for matching the third character which is not a digit and not an English alphabet
SELECT * FROM
mytable
where SUBSTR(mycol,3,1) NOT RLIKE '^[A-Za-z0-9]$';
I have a table where sometimes I need to replace the number at the beginning if it exists. How can I do this without matching the entire string I just want if the first 3 match?
Thanks
SELECT name, REPLACE(number, '338', '08')
from contacts
group by name
Use IF or CASE, and use substring operations instead of REPLACE (because REPLACE will do multiple replacements, not just at the beginning).
SELECT NAME, IF(number LIKE '338%', CONCAT('08', SUBSTR(number, 4)), number)
FROM contacts
GROUP BY name
you could use LEFT() for this then CONCAT end of string.
select concat(replace( left(number,3),'338','08'),substr(number,4))
but phone numbers could be written many ways. ( +33xxxx, (33) xxxxx, 0033xxxxx, ...) This is not so simple.
I have a table, one of the columns contains a text values, some of which are comma separated string, like this:
Downtown, Market District, Warehouse District
I need to modify my query to see is a given value matches this column. I decided that using IN() is the best choice.
SELECT *
FROM t1
WHERE myValue IN (t1.nighborhood)
I am getting spotty results - sometimes I return records and sometimes not. If there's a value in t1.nighborhood that matches myValue, I do get data.
I checked and there are no MySQL errors. What am I missing?
You can use FIND_IN_SET() to search a comma-delimited list:
SELECT *
FROM t1
WHERE FIND_IN_SET(myValue, REPLACE(t1.nighborhood, ', ', ','));
The REPLACE() is necessary to remove the extra spaces.
Another solution is to use regex to match your search value surrounded by commas if necessary:
SELECT *
FROM t1
WHERE t1.nighborhood REGEXP CONCAT('(^|, )', myValue, '(, |$)');
In general, it's bad design to store distinct values in a single column. The data should be normalized into a related table with a foreign key.
I'm trying to build a SQL statement to retrieve user names in the following order
at first, return the names that start with Arabic letter, then the names that start with English letters, then the names that start with special characters.
then sort each of the three groups in ascending order.
This is my code:
SELECT `name` FROM `user`
order by case when substring(name,1,1) like 'N[أ-ي]' then 1
when substring(name,1,1) like '[a-zA-Z]' then 2
else 3
end
,name
The problem is that the case part always returns 3, and so the statement sorts the names in the default order(special chars first, then English letters then Arabic letters). What is the problem in my query?
You need to use regex, not like... (because you use regular expression)
SELECT `name` FROM `user`
order by case when substring(name,1,1) regexp 'N[أ-ي]' then 1
when substring(name,1,1) regexp '[a-zA-Z]' then 2
else 3
end
,name
Reference: MySQL CASE statement and REGEXP
I have columns in a mysql table that stores names of people as combinations of strings and incremented digits for uniqueness, so I have names stored as so :
Patrick, Patrick1, Patrick2, ..... Patrick10, David, David2, .... David5
How do I retrieve just the alpha name itself, without the digits? Say I want to group by the distinct names, and count per group, so I get a result resembling the following.
name | frequency
-----------------
Patrick | 10
David | 5
A solution would be this:(it doesn't look to good, but it works)
SELECT
TRIM(TRAILING '0' FROM
TRIM(TRAILING '1' FROM
TRIM(TRAILING '2' FROM
TRIM(TRAILING '3' FROM
-- ...
TRIM(TRAILING '8' FROM
TRIM(TRAILING '9' FROM name)))))) AS name
FROM your_table
Then you can select with GROUP BY from the result:
SELECT name, count(*) AS frequency FROM (
-- previous select
) AS t
GROUP BY name
I'll have a little think about that, but I would recommend that if you need a distinguishing number, you keep it in a different column. That way, you won't have difficulties of this sort.
You can "chain" the replace command like this (this will remove the digits 0,1,2 in the query). You can expand this for the other digits, but I don't know if this will perform very well on large datasets:
select replace(replace(replace(Name,"0",""),"1",""),"2","") from users;
I would think also, it will be better to do what Brian suggested.
you could use a udf.
and then try Something like follwing
select REGEX_REPLACE(name, [0-9], '') as Name, Count(Name)
from tableName
Group by Name