I have to retrieve distinct entities from a column, all of which start with a vowel. The query looks like this :
Select DISTINCT column_name
FROM table_name
WHERE column_name LIKE '[aeiou]%';
It's not giving compilation errors, but it's not returning anything. Any ideas?
You can use regular expressions:
Select DISTINCT column_name
FROM table_name
WHERE column_name REGEXP '^[aeiou]';
LIKE wildcard patterns do not support character classes, except in a couple of databases that extend the definitions of the LIKE pattern.
Also, you might want:
WHERE column_name REGEXP '^[aeiouAEIOU]'
If you have a case-sensitive collation.
Related
Anyone have a clue how I could go about trying to select a certain pattern of numbers with a 1 at the end?
Ex.
SELECT pattern FROM table WHERE pattern REGEXP '1_2+2_2+3_2+4_2&2016-06-09&1';
or
SELECT pattern FROM table WHERE pattern REGEXP '2_1&2016-06-09&1';
using the same number-underscore-number, ampersand, date, ampersand, number; just as long as that number 1 is at the end?
EDIT:
Actually, let me phrase it better. How do I use REGEXP to select an ampersand and the number 1 at the end of a string?
You don't need regex. Just use LIKE:
LIKE '%&1'
The % makes it not be anchored to the start of the string. LIKE is not regex, but closer to a glob syntax. It may be faster than regex, too.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
SELECT column_name
FROM table
WHERE column_name LIKE '%&1';
Note:
You can also use LIKE operator for searching from start not only from end.
Here is an Example.
SELECT column_name
FROM table
WHERE column_name LIKE '&1%';
how to use like function which contains space as one of the character in msql.
In my database, i am having column name as "contractor_id".
This is my code for selecting column name from table.
SELECT COLUMN_NAME as a FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '$table' AND column_name LIKE '%".actor id."%'.
Just leave out the quotation marks like
SELECT COLUMN_NAME as a
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '$table'
AND column_name LIKE '%actor id%'
Or, in case you are generally looking for a column name with a blank in it use
... column_name like '% %'
Hello i need query for select all records from database containig &var separted by semicolons.
where &var is any number bellow which start with 1, like 123,166,1444
my varchar column look like this "123;166;234;1444"
the column may look and like this 233;166;12, which also containg numbers which start with 1.
select * from table_name where column_name like '1%' or column_name like '%;1%'
replace table_name with your tablename . Also replace column_name with your column name
I have a code which I recently discovered :) and it does do its job and well done I might add. But, I want to check all columns instead of checking it by column. Is it possible
Check my code below:
SELECT column_name
FROM table_name
WHERE column_name REGEXP '[[.DLE.]-[.US.]]'
Now, what I want is something like this but it won't work
SELECT *
FROM table_name
WHERE REGEXP '[[.DLE.]-[.US.]]'
Kindly advice and I apologize for asking many questions :)
REGEXP is a binary operator which means you have to have a left operand and a right operand.
Like most arithmetic operators.
You could check all columns like this:
SELECT *
FROM table_name
WHERE CONCAT(a, b, c, d, ...) REGEXP '[[.DLE.]-[.US.]]'
I'm using ... for the sake of the example, but you'd need to name all your columns explicitly. There's no option to use a wildcard for the columns inside an expression.
You can't set "all columns" in a single SET clause. You'd need to do something like the following:
UPDATE table_name SET
a = REPLACE(a,char(16),''),
b = REPLACE(b,char(16),''),
c = REPLACE(c,char(16),''),
d = REPLACE(d,char(16),''),
...similar for other columns;
If you think this is an unexpected omission in the SQL language, then I wonder if you can name any other programming language that lets you compare to or assign a value to "all variables" in a single expression?
I am trying to execute the sql query:
select * from table where column like '%value%';
But the data is saved as 'Value' ( V is capital ).
When I execute this query i don't get any rows.
How do i make the call such that, it looks for 'value' irrespective of the casing of the characters ?
use LOWER Function in both (column and search word(s)). Doing it so, you assure that the even if in the query is something like %VaLuE%, it wont matter
select qt.*
from query_table qt
where LOWER(column_name) LIKE LOWER('%vAlUe%');
If you want this column be case insensitive :
ALTER TABLE `schema`.`table`
CHANGE COLUMN `column` `column` TEXT CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';
Thus, you don't have to change your query.
And the MySQL engine will process your query quicker than using lower() function or any other tricks.
And I'm not sure that using lower function will be a good solution for index searching performance.
Either use a case-insensitive collation on your table, or force the values to be lower case, e.g.
WHERE lower(column) LIKE lower('%value%');
Try using a case insensitive collation
select * from table
where column like '%value%' collate utf8_general_ci
Use the lower() function:
select t.*
from table t
where lower(column) like '%value%';
you should use either lower or upper function to ignore the case while you are searching for some field using like.
select * from student where upper(sname) like 'S%';
OR
select * from student where lower(sname) like 'S%';
If you are using PostgreSQL, a simpler solution is to use insensitive like (ILIKE):
SELECT * FROM table WHERE column ILIKE '%value%'
I know this is a very old question, but I'm posting this for posterity:
Non-binary string comparisons (including LIKE) are case-insensitive by default in MySql:
https://dev.mysql.com/doc/refman/en/case-sensitivity.html
This will eventually do the same thing. The ILIKE works, irrespective of the casing nature
SELECT *
FROM table
WHERE column_name ILIKE "%value%"