MySQL IF begins with a number do X, else do Y [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL - If It Starts With A Number Or Special Character
Select values that begin with a number
I'm trying to create an ORDER BY statement that checks to see if the column begins with a number, if it does it should add + 0 to the column. If not, it does not.
Something like this IF(title begins with number, title + 0, title)
Not sure how to go about this.

ORDER BY IF(title REGEXP '^[0-9]',title+0,title)
But, that expression is going to return a numeric value; and since "title+0" is essentially equal to "title" (in terms of a numeric value comparison), I don't think that's going to do what you want it to.
(The "+0" operation will add zero to the leading numeric portion of the string value, and return a numeric value; so the entire expression will be evaluated as a numeric value. Which means that the bare "title" will also be a numeric value, which will be returned as 0 if there is no leading numeric.
So, that is essentially equivalent to:
ORDER BY title+0
In what order do you actually want the title values returned in? Do you want the rows returned in order by the leading numeric, and then by the title string?
I'm thinking you want to order by the string value of title when there isn't a leading numeric digit; otherwise, by the numeric value of leading numeric digits, and then the string value of title, so something like this might get you closer to what you want:
ORDER BY title REGEXP '^[0-9]', title+0, title

You could force the title to be interpreted as an integer and test that it's >= 1:
ORDER BY IF(title+0 >= 1, title+0, title)
But I have to comment that it sounds like you're using one column for two different purposes, which is a no-no in relational database design.
Also it's going to be really slow, since the ORDER BY can't benefit from an index.

if title LIKE 'number%' then
CONCAT(title, '0');
end if;

if (left(title,1) regexp '[0-9]', title+0, title)
or
if (title regexp '^[0-9]', title+0, title)

Related

How to get the values for which the format and suffix are known but the exact values are not known and there can be multiple values from the database?

I have a use case as below:
I have thousands of records in the database and let's say I am having one column named myValue.
Now the myValue's actual value can be an alphanumeric string where the first two characters are alphabets, the next 6 characters are numbers and the last character is a fixed alphabet let say 'x', which may be or may not be present in the value. (For Example 'AB123456','AB123456x')
So I know the format of the value for myValue field but not know all the actual values as there are lots of records.
Now I want to retrieve all such values for which the value without last character x (For Example, 'AB123456') and the same value with last character x (For Example, 'AB123456x') exists.
So is there any way I can retrieve such data?
I am right now doing trial and error on existing data but have not found success and there are thousands of such rows, so any help on this would be appreciated a lot.
You can do so like this:
SELECT myvalue
FROM t
WHERE myvalue LIKE '________'
AND EXISTS (
SELECT 1
FROM t AS x
WHERE x.myvalue = CONCAT(t.myvalue, 'x')
)
A (most likely) faster alternate is:
SELECT TRIM(TRAILING 'x' FROM myvalue) AS myvalue2
FROM t
GROUP BY myvalue2
HAVING COUNT(DISTINCT myvalue) > 1

MySQL "LIKE" without "%" is not working same as "=" condition [duplicate]

This question already has answers here:
Equals(=) vs. LIKE
(16 answers)
Why is there is a difference between equality comparison between equal operator and like operator?
(3 answers)
Difference between SQL LIKE without percent signs and equal (=) in WHERE clause
(2 answers)
Closed 4 years ago.
I have the below MySQL query.
select * from node where title LIKE 'born in care shelter breed';
Which is returning empty set. But when I try the below query
select * from node where title = 'born in care shelter breed';
It is returning 1 result.
What difference the both will make? I can't avoid the LIKE operator as the query creating after some condition checking
I'm guessing you have trailing whitespace in your title field. MySQL string comparison using the = sign does not consider trailing whitespace. Note the following:
CREATE TABLE node
(title VARCHAR(99));
INSERT INTO node (title)
VALUES ('born in care shelter breed '); -- Note the space at the end
SELECT * FROM node WHERE title LIKE 'born in care shelter breed';
SELECT * FROM node WHERE title = 'born in care shelter breed';
Notice how the first select statement returns zero results, but the second one finds the row.
SQL Fiddle
The documentation talks about this on the string comparison page, stating:
In particular, trailing spaces are significant, which is not true for
CHAR or VARCHAR comparisons performed with the = operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards used in conjunction with the LIKE operator:
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
i think below will return row
select * from node where title LIKE '%born in care shelter breed%';
To know about like more

Is it possible in MySQL to search a table for where a column only contains 1 comma?

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 '^[^,]*,[^,]*$'

update specific column of mysql table

I have a quite big table in mysql and I need to change all the records related to this column.
records are like this :
/name/nm0000209/?ref_=ttfc_fc_cl_t1,
/name/nm0000151/?ref_=ttfc_fc_cl_t2,
...,
/name/nm0104594/?ref_=ttfc_fc_cl_t10
what I want is to keep only the string in the middle which is nm0000209, nm0000151,.... I know how to delete specific characters from the right or left of the words by REPLACE or Trim , .., but my problem is that in this case the number of characters in the third part of string are not equal (as you see when it reaches to 10, I have to delete 21 characters from the end instead of 20 characters and since this table contains lots of records I dont know how to do it.
I reaaly appreciate if someone could helop me,
thanks
I want is to keep only the string in the middle which is nm0000209, nm0000151...
You can use 'SUBSTRING_INDEX' on the column to crop part of the column value.
Following example assumes that the said column will have 'name/' as starting pattern.
Example:
update table_name
set column_name = substring_index(
substring_index( column_name, 'name/', -1 )
, '/', 1 );
The same can be used for updating with the same value.
Demo # MySQL Fiddle
One approach would be to use MYSQL's SUBSTRING_INDEX function. It would let you get whatever's after the last slash. Or after the second to last.
For your particular case
select
SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
from supertext
would yield the desired result
EDIT: for update purposes
UPDATE thetable
SET thefield=SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)

MySQL : How to remove string from column

Very simple
I have a column of string like that :
abc1
abc2
abc3
abc4
abc5
I need to remove 'abc' from this column and keep only the number on the right, so column would have only the number like :
1
2
3
4
5
I thought about smth like that but it doesn't work
update table2
-> set col1 = case when 'abc1' then 1
-> else end;
I know how to concat text, I don't know how to undo it... Help please ^^;
#McArthey already hinted at it, but this is easy to do when the "abc" is consistently "abc" (i.e. the length doesn't change.)
Amongst the various string functions is one in particular: RIGHT(). This allows you to select a fixed number of characters from a string. e.g.
SELECT RIGHT('abc3',1) -- Results in "3"
SELECT RIGHT('abc3',2) -- Results in "c3"
Coupled with the LENGTH() function, you can conclude the numbers are anything past the 3rd character. i.e.
SELECT RIGHT('abc3',LENGTH('abc3')-3) -- Results in "3"
Obviously I'm using hard strings ('abc3'), but these can easily be replaced with column names.
The caveat here is that these all are based on fixed length letter prefixes. The more variable (changing) the "abc" in your example is, the harder picking the numeric value out of the column becomes.
If these are single digit values you could use
select right(column,1) ...
You may also find the REGEXP docs useful if it is more complex.
If you are trying to modify the column you will have to take the values separately and then concatenate them back together. It's difficult to give a precise answer since I don't know what you're trying to accomplish but you could do something with SUBSTR to grab the separate values.
Get 'abc': SUBSTR(column, 1,3)
Get digits: SUBSTR(column, 4)