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.
Related
I suck at REGEX, but I need to pull all the records from a table column that stats with AST, and the rest only contains numbers after. I am assuming this can be done with just REGEX and not LIKE but I'm not sure.
For instance AST000001
and not AST99XXH011
SELECT * FROM table WHERE column LIKE 'AST%' AND column REGEXP '[0-9]$'
You can use REGEXP/RLIKE on the whole column value (using start-of-string (^) and end-of-string ($) anchors to ensure you match the entire column):
SELECT *
FROM `table`
WHERE `column` REGEXP '^AST[0-9]+$'
Demo on dbfiddle
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 am trying to find all values that do not start with a number
I have tried this query, but not sure if the REGEXP is correct. This seem to be returning any value the does not contains a number
SELECT * FROM table where address NOT REGEXP '[0-9]'
I think this fixed the issue
SELECT * FROM table where address NOT REGEXP '^[0-9]'
In MySQL if you try to convert a string to a number then it will start from the beginning taking the digits and convert as much as possible.
If a string does not start with a number then the result is 0.
select * from your_table
where address * 1 = 0
SQLFiddle demo
You can use explicit conversion with cast(str_column as unsigned) or implicit conversion by using a mathematical operator like * 1
I want to truncate the first letter of a string which have size more than 11 character.
I know I can use substring function like
SELECT SUBSTRING(name, 1, 10) from table1;
which will truncate and return me the first 10 letter. what should I do if I want to remove the character from the beginning if the string is greater than 10 character.
abcdefghijklmn ==> efghijklmn
How about RIGHT():
SELECT RIGHT(name, 10)
FROM table1;
Demo: SQL Fiddle
RIGHT() returns a specified number of characters from the right side of a string.
If you want to apply any function only in certain situations, a CASE statement can be used.
create table table1(name char(25));
insert into table1 values('abcdefghijklmn');
select right(name,10) from table1;
RIGHT() is the function you need to use.
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.