MySQL Cannot Compare TEXT - mysql

I have a MySQL table called table_1 that has column named col_1 of type TEXT. I am trying query a row that has a certain value using this statement, but no data is returned.
SELECT * FROM table _1 WHERE col_1 = '1325'
However, when I used this statement, I do get data.
SELECT * FROM table_1 WHERE col_1 LIKE '%1325%'
How do I get the first statement to work?

If the select with the WHERE col_1 LIKE '%1325%' brings back a value that looks like 1325, but then WHERE col_1 = '1325' doesn't?
Then it's probably because of invisible characters.
For example if the value of col_1 is '1325 '.
Because of the extra space, that value isn't equal to '1325'.
But it does contain '1325', so the LIKE would find it.
It's easy not to notice what's not visible.
What you could do is TRIM the value, so that spaces are removed before comparing it with =.
SELECT * FROM table_1 WHERE TRIM(col_1) = '1325';
Note that a default TRIM or RTRIM removes only the spaces.
To make that work for other whitespace characters the SQL becomes be a bit longer.
SELECT * FROM table_1
WHERE TRIM(Replace(Replace(Replace(col_1,'\t',''),'\n',''),'\r','')) = '1325';
And if you want to have visual evidence wether this is caused by invisible whitespaces?
Then use the LIKE, and CONCAT something to the start and end of the column. Then the spaces will be easy to notice in the result.
select CONCAT('[',col_1,']') AS Test, t.* from table_1 t where col_1 like '%1325%';
You can test it here

Because you do not test with:
- **SELECT * FROM table _1 WHERE col_1="1325" **

the second statment means the data contains 1325 but you don't have a row containning 13
Hope you got iT thanks

Related

Column that starts with AST and ends in only number

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

mysql match special character % from a text

I have to select all rows from a table where column (varchar) values contain '%' in the text.
I tried below query but failed to get correct result set.
SELECT * from TABLE where VALUE LIKE '%%%'
Above query gives all rows of the table.
Please help me to form a query to match '%' and get the correct results.
SELECT * FROM your_table
WHERE value LIKE '%\\%%'
SQLFiddle demo
Escape character worked for me.
SELECT * from TABLE where VALUE LIKE '%\%%'
This query gives me correct result set.

Left function leaving out characters

I have a database with over a 1,000 numbers in roughly the same format: aaa-111-2222
Some of the numbers have extra parts at the end: aaa-111-2222(bbbb)
I created a column and updated my table using the left function:
UPDATE table_1
SET col_2 = Left(col_1, 12)
I want to update the entire table so I didn't include a WHERE function.
The problem is when it updates, it is leaving off two characters.
Col_1: aaa-111-2222(bbbb)
when the function was ran it returned:
Col_2: aaa-111-22
Which is short characters. I thought that there might be some leading spaces I couldn't see, so on the orginal column I:
SELECT REPLACE(Col_1, " ","") FROM table_1
Then I reran my update and it was still returning short. I thought maybe I had extra returns and other white space, so I ran:
SELECT REPLACE(REPLACE(Col_1, CHAR(13), ''), CHAR(10), '') FROM table_1
Then I deleted Col_2 and remade it again. The structure was VARCHAR, with a Length of 15 (Which is 3 more than I should have)
After I ran the UPDATE again, it is still short 2 characters.
Any ideas on what I else I can do to fix?
May be there are leading spaces in col_1. The REPLACE did not update col_1 as it only applies the resulting dataset of the SELECT. Try use TRIM to eliminate the leading and trailing spaces to see if it makes any difference:
UPDATE table_1
SET col_2 = Left(TRIM(col_1), 12) ;
Also try this:
UPDATE table_1
SET col_2 = LEFT(TRIM(REPLACE(REPLACE(col_1,'\r',''),'\n','')), 12);
I ended up noticing that my table was 3 times bigger than it should have been. I emptied the table, reloaded it with backup data I had, then did the REPLACE and after that it worked just fine.
Thank you guys for the tips.

MySQL - WHERE x IN ( column)

I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it

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