I need to eliminate the result of the Select statement
if a certain amount of numbers appear at the end.
Example:
select name from table where registration = '1234'
if the data record is:
Joe Bin12345
I need the result to lose its registration number when printing:
Joe Bin
The result should loose its digits
only if it contains five numeric characters.
SELECT name,
REGEXP_REPLACE(name, '[0-9]{5}$', '') cleared
FROM test;
fiddle
Related
create table numbers (number varchar(10));
insert into numbers (number) values
('1234123452'),
('5532003644'),
('1122330505'),
('1103220311'),
('1103000011'),
('1103020012');
Query:-
SELECT * FROM numbers
WHERE SUBSTRING(Number,1,4) = SUBSTRING(Number,5,8)
Result:-
There are no results to be displayed.
Expected Result:
1234123452
The third argument to SUBSTRING() is the length, not the ending position. So it should be:
SELECT * FROM numbers
WHERE SUBSTRING(Number,1,4) = SUBSTRING(Number,5,4)
You could take advantage of REGEXP_LIKE here, assuming you are using MySQL 8+:
SELECT *
FROM numbers
WHERE REGEXP_LIKE(Number, '^(.{4})\\1';
The pattern ^(.{4})\\1 matches and captures the first four characters, then asserts that these same characters appear immediately afterward.
I have a field called 'areasCovered' in a MySQL database, which contains a string list of postcodes.
There are 2 rows that have similar data e.g:
Row 1: 'B*,PO*,WA*'
Row 2: 'BB*, SO*, DE*'
Note - The strings are not in any particular order and could change depending on the user
Now, if I was to use a query like:
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%'
I'd like it to return JUST Row 1. However, it's returning Row 2 aswell, because of the BB* in the string.
How could I prevent it from doing this?
The key to using like in this case is to include delimiters, so you can look for delimited values:
SELECT *
FROM technicians
WHERE concat(', ', areasCovered, ', ') LIKE '%, B*, %'
In MySQL, you can also use find_in_set(), but the space can cause you problems so you need to get rid of it:
SELECT *
FROM technicians
WHERE find_in_set('B', replace(areasCovered, ', ', ',') > 0
Finally, though, you should not be storing these types of lists as strings. You should be storing them in a separate table, a junction table, with one row per technician and per area covered. That makes these types of queries easier to express and they have better performance.
You are searching wild cards at the start as well as end.
You need only at end.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Reference:
Normally I hate REGEXP. But ho hum:
SELECT * FROM technicians
WHERE concat(",",replace(areasCovered,", ",",")) regexp ',B{1}\\*';
To explain a bit:
Get rid of the pesky space:
select replace("B*,PO*,WA*",", ",",");
Bolt a comma on the front
select concat(",",replace("B*,PO*,WA*",", ",","));
Use a REGEX to match "comma B once followed by an asterix":
select concat(",",replace("B*,PO*,WA*",", ",",")) regexp ',B{1}\\*';
I could not check it on my machine, but it's should work:
SELECT * FROM technicians WHERE areasCovered <> replace(areaCovered,',B*','whatever')
In case the 'B*' does not exist, the areasCovered will be equal to replace(areaCovered,',B*','whatever'), and it will reject that row.
In case the 'B*' exists, the areCovered will NOT be eqaul to replace(areaCovered,',B*','whatever'), and it will accept that row.
You can Do it the way Programming Student suggested
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
Or you can also use limit on query
SELECT * FROM technicians WHERE areasCovered LIKE '%B*%' LIMIT 1
%B*% contains % on each side which makes it to return all the rows where value contains B* at any position of the text however your requirement is to find all the rows which contains values starting with B* so following query should do the work.
SELECT * FROM technicians WHERE areasCovered LIKE 'B*%'
I need to search a string in a mysql field for a substring and return the number to the right of it, if it is the highest number of all the rows returned. (The word will most likely occur many times in the string.)
SELECT max(cast(substring(mq.my_query_string, (locate("near/", mq.my_query_string)+5), 2) as signed)) AS "MaxNearDistance"
FROM my_query mq, my_test mt
WHERE mt.company_id = 123
AND mq.search_query_id = mt.my_query_id
AND locate("near/", mq.my_query_string) > 0;
This works, but it only gives me the highest number of the FIRST substring and among all rows returned. I need the highest number among ALL hits for the substring and among all rows.
Thank you for your help!
I would create a table- numbers- with every number string I want to be able to find ('0' - '99' for you) then join it to my data like:
SELECT
mq.my_query_string, MAX(numbers.ns)
FROM my_query AS mq
JOIN numbers ON CONCAT(mq.my_query_string, ' ') LIKE CONCAT('%near ', numbers.ns, ' %')
GROUP BY mq.my_query_string
I'm CONCATing a space to the data so that '11' won't match '1'.
SQLFiddle here: http://sqlfiddle.com/#!2/8fda1c/4
(Actually, the right way is to normalize your data so you have a table of my_query_nears with one value per row but sometimes we must play the hand we are dealt.)
I need to order by a field that contains a set of numbers. Lets say a table named TEST contains ID, NAME, QUADS with QUADS as follows.
95,273,212,405
717,450,771,504
391,176,646,272
This are the results I am getting with a query such as
SELECT * FROM TEST ORDER BY QUADS
391,176,646,272
717,450,771,504
95,273,212,405
These are the results I am looking to get
95,273,212,405
391,176,646,272
717,450,771,504
I am only interested in the first number in the set for "order". Figure it might be possible with a substring to the comma but not sure how to do that in MySQL.
Try this:
select * from test
order by cast(substring_index(quads,',',1) as unsigned)
What you want is the substring_index function.
... order by substring_index(x_field,',',1)
This extracts the text in x_field up to the first occurrence of the comma delimiter
Try with this:
select QUADS, 0+QUADS as S from TEST order by S
0+QUADS will convert your string to int and will use for it just the first digits sequence before "," which is actually what you want.
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