I currently have a column in a table that stores every number combination up to 6 digits followed by .lyr
Eg, 0.lyr ,1.lyr, 00.lyr, 11.lyr - 999999.lyr
I want to be able to write a query that shows me all 4 digit combinations: 0000.lyr-9999.lyr. Is there anything I can do without needing to use an in-statement?
Simply convert your value to numeric one and check.
SELECT *
FROM tablename
WHERE 0 + columnname BETWEEN 0 AND 9999;
If 0.lyr and 0000.lyr are different values and you need only the row with 2nd value then use rregular expression check.
SELECT *
FROM tablename
WHERE columnname REGEXP '^\\d{4}.lyr$';
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 MySQL table containing 10 digit numbers. I need to add +1 in front of each via an UPDATE.
Let's say my SELECT statement looks like this:
SELECT *
FROM num_data
WHERE number REGEXP '^[0-9]{10}$'
How do I add +1 in front of each result of my query above?
Use CONCAT to concatenate strings in MySQL.
UPDATE num_data
SET number = CONCAT('+1', number)
WHERE number REGEXP '^[0-9]{10}$'
I have this column in a table which is comma delimited to separate the values.
Here's the sample data:
2003,2004
2003,2005
2003,2006
2003,2004,2005
2003,2007
I want to get all data that contains only 1 comma.
I've been playing around with the '%' and '_' wildcards, but I can't seem to get the results I need.
SELECT column FROM table WHERE column like '%_,%'
Replace the , with '' empty set then take the original length less the replaced length. if 1 then only 1 comma if > 1 then more than 1 comma.
The length difference would represent the number of commas.
Length(column) - length(Replace(column,',','')) as NumOfCommas
or
where Length(column) - length(Replace(column,',','')) =1
While this may solve the problem, I agree with what others have indicated. Storing multiple values in a single column in a RDBMS is asking for more trouble. Better to normalize the data and get it to at least 3rd Normal form!
You can also use find_in_set() method which searches a value in comma separated list, by picking the last value of column using substring_index we can then check result of find_in_set should be 2 so that its the second and last value from list
select *
from demo
where find_in_set(substring_index(data,',',-1),data) = 2
Demo
Maybe another solution is to use regular expression in your case it can look like this ^[0-9]{4},[0-9]{4}$ :
SELECT * FROM MyTable WHERE ColName REGEXP '^[0-9]{4},[0-9]{4}$'
Or if you want all non comma one or more time :
SELECT * FROM MyTable WHERE ColName REGEXP '^[^,]*,[^,]*$'
I have data set of about 10K alphanumeric words with 10 characters length each. I need to match these using the first 3 characters and the last 3 characters.
Example: BGP12BR2010
In this case, I should use only BGP and 010 and see if there are any entries in my database. I have used
LEFT(replace(term_id,' ',''),3)||RIGHT(replace(term_id,' ',''),3)
Is there any other way to get this done.
You can also use LIKE:
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP%210';
this matches on all string, not only 10 CHAR. to specify the lenght you can
use underscore
SELECT * FROM yourTabel WHERE term_id LIKE 'BGP____210';
A better way for this is to add 2 virtual persitent fields, where Mysql calculate the values and you also can set a index on it for a better performance and not using a full table scan
add persistent virtual fields
ALTER TABLE yourtable
ADD COLUMN first3 VARCHAR(5) AS (SUBSTRING('hallo',1,3)) PERSISTENT,
ADD COLUMN last3 VARCHAR(5) AS (SUBSTRING('hallo',-3,3)) PERSISTENT;
Now you can select it
SELECT * FROM yourTable where first in('BGP','YXZ','XXX) and last3 = '210';
I'll do so:
SELECT * FROM yourtable
WHERE LENGTH(yourcolumn) = 10
AND yourcolumn LIKE 'BPG%010';
To get all the values starting with 3 alphabets and ending with 3 numeric characters, use
select *
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
To extract them, if they follow the above pattern,
select val, substring(val,1,3) as first3, substring(val,-3,3) last3,
--concatenate them if required
concat(substring(val,1,3), substring(val,-3,3)) concatenated_string
from t
where val regexp '^[a-z]{3}.+[0-9]{3}$'
Add a condition for length of the column if it has to be exactly 10 characters. In that case, change the regexp to '^[a-z]{3}.{numcharactersrequired}[0-9]{3}$' , which would be '^[a-z]{3}.{4}[0-9]{3}$'
SQL Fiddle
For example I have a table column : lorem,ipsum,mini,momo,testing
I need to select total 'Comma' 's count.
Is it possible to query it as result of : 4 because having 4 comma in the column ?
Does mysql have any trick like select character length by filtering certain words like :
SELECT CHAR_LENGTH(section,',') FROM table WHERE 1 = 1 ?
You are close. Just use subtraction:
select length(section) - length(replace(section, ',', ''))
However, you shouldn't be storing lists of things in strings. Instead, use a junction table with one row per item.