Mysql LIKE 2nd character and 2nd last character both are 'm' - mysql

I try to find query to find a string that 2nd character and 2nd last character both are letter m.
SELECT last_name
FROM employees
WHERE (last_name LIKE '_m%m_' AND LENGTH(last_name) >= '3');
Thanks in advance :)

Why not just OR instead of AND? I don't see the point of AND when your LIKE operator allready rules out names below three characters. You don't need to use regex nor a check for length:
SELECT last_name FROM employees
WHERE last_name LIKE '_m_'
OR last_name LIKE '_m%m_';
The use of OR and LIKE does catch any string that has at least 3 characters.
If you must use regex, try REGEXP operator:
SELECT last_name FROM employees WHERE last_name REGEXP '^.m(.*m)?.$';
Where the pattern means:
^.m - Start-line anchor with a single character and a literal 'm';
(.*m)? - Optional capture group to match 0+ characters upto a literal 'm';
.$ - A single character with end-line anchor.
The benefit of REGEXP is that it's a bit less verbose if you need case-insensitive matching using pattern: '^.[Mm](.*[Mm])?.$'. See an online demo.

If you need all record with second and last character is m you can use the following query:
select * from <table> where <column> like '_m%m'
the _ in the query is a placeholder for one character and % for many characters

Related

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%'

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.

How to use SQL to remove superfluous characters from names?

How do I remove all superfluous full-stop . and semi-colon ; characters from end of last name field values in SQL?
One way to check of the last character is a "full stop" or "semicolon" is to use a substring function to get the last character, and compare that to the characters you are looking for. (There are several ways to do this, for example, using LIKE or REGEXP operator.
If that last character matches, then lop off that last character. One way to do that is to use a substring function. (Use the CHAR_LENGTH function to return the number of characters in the string.)
For example, something like this:
UPDATE mytable t
SET t.last_name = SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1)
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
But, I'd strongly recommend that you test those expressions using a SELECT statement, before running an UPDATE statement.
SELECT t.last_name AS old_val
, SUBSTR(t.last_name,1,CHAR_LENGTH(t.last_name)-1) AS new_val
FROM mytable t
WHERE SUBSTRING(t.last_name,CHAR_LENGTH(t.last_name),1) IN ('.',';')
Substring rows that have a semi-colon or dot :
update emp
set ename = substring(ename, 1, char_length(ename) - 1)
where ename REGEXP '[.;]$';

Mysql SELECT all rows where char exists in value but not the last one

I need a SELECT query in MYSQL that will retrieve all rows in one table witch field values contain "?" char with one condition: the char is not the last character
Example:
ID Field
1 123??see
2 12?
3 45??78??
Returning rows would then be those from ID 1 and 3 that match the condition given
The only statement I have is:
SELECT *
FROM table
WHERE Field LIKE '%?%'
But, the MySQL query does not solve my problem..
The LIKE expressions also support a wildcard "_" which matches exactly one character.
So you can write an expression like the example below, and know that your "?" will not be the last character in the string. There must be at least one more character.
WHERE intrebare LIKE '%?_%'
Re comment from #JohnRuddell,
Yes, that's true, this will match the string "??" because a "?" exists in a position that is not the last character.
It depends whether the OP means for that to be a match or not. The OP says the string "45??78??" is a match, but it's not clear if they would intend that "4578??" to be a match.
An alternative is to use a regular expression, but this is a little more tricky because you have to escape a literal "?", so it won't be interpreted as a regexp metacharacter. Then also escape the escape character.
WHERE intrebare REGEXP '\\?[^?]'
you can just add an additional where where the last character is not a ?
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND intrebare NOT LIKE '%?'
you could also do it like this
SELECT *
FROM intrebari
WHERE intrebare LIKE '%?%' AND RIGHT(intrebare,1) <> '?'
DEMO

My sql:how to use Wildcard in Subquery?

example:
SELECT country
FROM data
WHERE city LIKE
(SELECT LEFT ('jakartada',7));
it's working nicely but if i give delimiter with value:4 ,i must give wildcard
like this ---> "%string%" ,where i can give wildcard in the query?
With LIKE you can use the following two wildcard characters in the pattern.
Character Description
% Matches any number of characters, even zero characters
_ Matches exactly one character
\% Matches one “%” character
\_ Matches one “_” character
does this query help you?
SELECT country
FROM data
WHERE city LIKE CONCAT ('%', (SELECT LEFT ('jakartada',7)), '%');
why you using sub query?
SELECT * FROM `data` WHERE city LIKE 'jakar%'