Regex Error in MYSQL - mysql

I want to select cities starting with a,e, i,o,u and ending with a,e, i,o,u in MySQL.(Case not matters)
Query1
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU]' and CITY REGEXP '[AEIOU]$';
Query2
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU]*[AEIOU]$';
Why Query2 is giving me an error although Query1 is correct.

With your first query, you only fetch entries that start or end with vowels. The second one only matches entries that start with 0 or more vowels and end with a vowel (so, you will get results like a or Aou only).
You might try using
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU].*[AEIOU]$'
^^
The .* pattern matches any 0+ chars, as many as possible, so it will matching any string that starts AND ends with a vowel.
However, WHERE CITY REGEXP '^[AEIOU]' and CITY REGEXP '[AEIOU]$' fetches entries only consisting of 1 vowel, and the above will not match a record like A (one-vowel string). To match those use an optional group:
SELECT CITY FROM STATION WHERE CITY REGEXP '^[AEIOU](.*[AEIOU])?$'
^ ^^
Here, (...)? is a capturing group (MySQL regex does not support non-capturing ones) that matches a sequence of patterns 1 or 0 times (due to the ? quantifier).
A couple of notes on the regex:
^[AEIOU].*[AEIOU]$ - matches a whole string that starts and ends with a vowel in a case insensitive way (REGEXP is not case sensitive, except when used with binary strings)
^ - matches the start of input
[AEIOU] - a single vowel from the set
.* - any 0+ chars as many as possible (POSIX regex used in MySQL does not support lazy quantifiers, and . matches any chars, even line break chars, too)
[AEIOU] - a vowel
$ - end of input.

^ : Match the beginning of a string.and $ : Match the end of a string.
so you can try with above both regex and also use % , may be helpful.

Related

MySQL 8 + Regex Word Boundaries

I want to searching for the term 'ed' at the start or end of a word, the following SQL statement only matches a exact word match.
SELECT * FROM ul_product
where productname REGEXP '\\bed\\b'
If I do the following it gets results where ed is at the start or end of a word
SELECT * FROM ul_product
where productname REGEXP '(\\bed)|(ed\\b)'
Is this how it's supposed to work?
The description of word boundary and examples online led me to believe statement 1 would produce the results of statement 2.
I can use the statements I've created as is for my 'exact' and 'partial' matching, but is this right?
Regex '\\bed\\b' searches for 'ed' surrounded by word boundaries - in other words it searches for word 'ed'.
On the other end, regex: '(\\bed)|(ed\\b)' searches for either '\\bed' or 'ed\\b' (the pipe character stands for "or" in regexes). So it matches on 'ed' at the beginning of a word or at the end of a word - which seems to be what you want.
Note that the parentheses are not necessary here. You could just write this as:
where productname REGEXP '\\bed|ed\\b'

MySQL command to get first letter of last name

Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'

Understanding why querying cities with vowels at start and end doesnt work

Task:
Query the list of names from table which have vowels as both their first and last characters [duplicate].
I want to query the list of CITY names from the table STATION(id,city, longitude, latitude) which have vowels as both their first and last characters. The result cannot contain duplicates.
My query:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY LIKE '[aeiou]%' AND '%[aeiou]'
I found this solution:
Select distinct city
from station
Where regexp_like(city, '^[aeiou].*[aeiou]$','i');
Why isn't my query working?
'[aeiou]' is a regex character class, which is not supported by operator LIKE. So your query won't do what you expect: it actually searches for a litteral string that starts with '[aeiou]' (and even if it was, you would need to repeat expression city like ... twice: city like ... and ... does not do what you expect either).
The solution you found uses regexp_like() with the following regex: ^[aeiou].*[aeiou]$, which means:
^ beginning of the string
[aeiou] one of the characters in the list
.* a sequence of 0 to N characters
[aeiou] one of the characters in the list
$ end of the string
Option 'i' makes the search case insensitive.
This works, but requires MySQL 8.0. If you are running an earlier version, consider using a REGEXP condition instead:
CITY REGEXP '^[aeiou].*[aeiou]$'

how to use keywords to search column in sql

I need to find all the songs which has 'ing' in song_title.
So, I write following code:
select *
from song
where song_title = 'ing'
But, that shows all the song_title which name is 'ing'.
How to get all that contain's 'ing' in song_title?
Use LIKE operators for pattern matching as follows:
SELECT* FROM song WHERE song_title LIKE '%ing%'
If you want to select records that start with a certain string then you can use:
SELECT* FROM song WHERE song_title LIKE 'ing%'
If you want to select records that end with a certain string then you can use:
SELECT* FROM song WHERE song_title LIKE '%ing'
As an alternative to a LIKE comparison, MySQL also has a REGEXP comparison operator. To return only rows where song_title contains the string 'ing'
WHERE song_title REGEXP 'ing'
With a LIKE comparison, the wild card characters are '%' to match any number (zero, one or more) of any character, and the underscore '_' to match one character. To search for literal '%' or '_' character, those characters need to be escaped.
With the REGEXP, there's a whole boatload of characters that have special meaning. e.g. '^' matches the beginning of the string, '$' matches the end of the string, and the square brackets, and all those other characters frequently used in regular expressions.
select * from song where song_title like '%ing%';
Use the like operator with substitution strings.

MySQL wildcard query

I'm relatively new with SQL and I ran into a problem/question while doing some practice problems.
https://www.hackerrank.com/challenges/weather-observation-station-8
Here's the query I used with MySQL:
SELECT DISTINCT CITY FROM STATION WHERE CITY LIKE '[aeiou]%[aeiou]'
I'm confused why this doesn't work. Here's my thinking for the query:
SELECT DISTINCT CITY
^ make sure each city returned isn't repeated
FROM STATION
^ from the STATION table
WHERE CITY LIKE '[aeiou]%[aeiou]'
^ CITY names that are selected can have the first letter begin with [aeiou], have anything in between, and end with [aeiou].
Any help or advice would be appreciated, thanks!
If you are using regex, you can use regexp or RLIKE in place of LIKE. The other thing you need to do is put ^ to denote the first character, $ to denote the last character, and .* for wildcard. See this and this:
SELECT DISTINCT CITY
FROM STATION
WHERE CITY RLIKE '^[aeiou].*[aeiou]$'
The like operator has only 2 wildcard characters: % and _, it does not handle the bracket syntax. You need regular expressions for that, and you can use the rlike operator for that. But in regular expressions you need to use . instead of _ and .* instead of %.