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.
Related
I have a column which stores the data from the below format.
["12973","111","5555"].
I want to select the items which is not in the above set.
for ex,
If I search 12973 if it is exactly match it will not return.
When I use Like if I search "129" it also return the same result.
Any ideas to solve this problem..
If you are searching for an exact number you can use NOT LIKE to exclude the row
SELECT * FROM test WHERE col1 NOT LIKE '%"12973"%'
if you are searching for a row where no numbers start with 129 you can use REGEXP
SELECT * FROM test WHERE NOT col1 REGEXP '.*"129[0-9]*"'
What about search all results with LIKE and remove the exact matches?
SELECT * FROM t WHERE a LIKE '%129%' AND a != '129'
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
I have value in my db
like
1,12,13,25,44,414,2114
I have to find exact 14 from the db.
but it also return the value 414 and 2114
but i want exact 14.
How can i achieve this through sql query Please help
i have tried this but didn't worked.
Select * from tb_name where columnNAme like '%value%'
If you want search exactly 14, then you need search %,14,%, but 14 may appears at start or at end of string, so you need add commas to column also.
Well, you can use:
select * from tb_name where concat(',',columnNAme ,',') like '%,14,%'
Side note, comma separated values in one column is bad database design
Select * from tb_name where columnNAme like 'value'
% - The percent sign represents zero, one, or multiple characters
SELET * FROM database WHERE column LIKE 14 should work fine, just double checked it on my database.
If this doesn't work can you show your query please?
Above answer is correct, I didn't see your query before I posted this.
When I run a query like this:
SELECT * FROM table WHERE columnName = AES_ENCRYPT('value','SecretKey')
I'm returned an empty set, even though there are rows in the db that match the search query.
What would the correct syntax for something like this look like?
SELECT * FROM table WHERE columnName = AES_ENCRYPT(columnName,'SecretKey')='value'
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.