I want to search in my database "emp" for names which have the alphabet "A" as third alphabet from left in their names.
This is what I tried:
select empname from emp
where left(empname,3)="a"
order by empname;
It returns an empty set.
Can you check my syntax, whether it is correct or not!
(Upvote promised ;) )
left(empname,3) will return the three leftmost characters in the string.
You need substring(empname,3,1), which will start at the third character and return a string of length 1.
The reference is here
Your query becomes:
select empname from emp
where substring(empname,3,1)="a"
order by empname;
The INSTR() function returns the position of the first occurrence of a string in another string. This function performs a case-insensitive search.
Select INSTR(first_name, 'm') from Tablename where first_name = 'monika';
Reference:- https://www.w3schools.com/sql/func_mysql_instr.asp
Related
A table, named strings, contains various strings, and unique id's:
Question is, how to write a query that takes an input string, and return the id of the longest matching (sub) string in the strings table?
The matching string, may, or may not be a substring of the input string. Comparison starts at string index 0.
Inputs and expected output:
INPUT -> OUTPUT
ABC -> 1
ABCD -> 2
ABCKK -> 1
ABCDDD -> 2
DAB -> NULL
CDE -> NULL
Doing this:
SET #theString = 'ABCBBB';
SELECT id FROM strings WHERE a_string LIKE #theString;
only returns the correct result when the input string exactly matches a string in the table, and don't work when the 'a_string' is a substring.
You can use:
select s.*
from strings s
where #theString regexp a_string
order by length(a_string) desc
limit 1;
In regards of using LIKE, you need to set the wildcards for it to work as the filter you want. If you are not required to set a variable, you can use the following query.
SELECT id FROM strings
WHERE a_string LIKE '%ABC%'
ORDER BY length(a_string) DESC LIMIT 1;
or if you need a variable, it can be done with the CONCAT function
SELECT id FROM strings
WHERE a_string LIKE CONCAT('%',#theString,'%')
ORDER BY length(a_string) DESC LIMIT 1;
This just is an alternative to #Gordon Linoff's answer.
I have the following Varchar data in a column in my MYSQL table:
Blank_Person_ID_776
Person_999
I want to extract the final number after the underscore to a variable (in this case 776) in order to use it in a query. I.e. ignore any underscore but the last one.
How can I do so?
I would like my final query to be as follows:
SET #personId= //query to get id;
Update Person set tracking_id = #personId where tracking_id is null;
If you want the final value after the last '_', use substring_index():
select substring_index(<whatever>, '_', -1)
If you specifically want the final number in the string, even when there are characters after:
select regexp_replace(<whatever>, '.*_([0-9]+)[^0-9]*$', '$1')
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]$'
I am trying to select all client names without vowels from a table (should therefore return an empty list) using the setminus operator with regular expressions, but it is simply returning the entire column. The same happens if I try to select all client names without 'a' or 'e' or any other vowel.
This is the query I'm using:
select client_name from client
where client_name regexp '[^aeiou]';
If I try doing a condition like below, then the inside caret actually does take every character other than 'a'. I'm not sure why it doesn't work by itself though.
select client_name from client
where client_name regexp '^[^a]'
Expected - empty output
Actual Results - whole column is returned
The regular expression can match anywhere in the name. So it will match any name that has any non-vowel character, not where all the characters are not vowels. You need to anchor it and quantify it:
WHERE client_name REGEXP '^[^aeiou]*$'
This tests all the characters in the name.
Or you can negate the test:
WHERE client_name NOT REGEXP '[aeiou]'
The regexp matches a vowel anywhere in the name. Then using NOT makes this return the names that don't match.
I have a simple task where I need to search a record starting with string characters and a single digit after them. What I'm trying is this
SELECT trecord FROM `tbl` WHERE (trecord LIKE 'ALA[d]%')
And
SELECT trecord FROM `tbl` WHERE (trecord LIKE 'ALA[0-9]%')
But both of the queries always return a null record
trecord
-------
null
Where as if I execute the following query
SELECT trecord FROM `tbl` WHERE (trecord LIKE 'ALA%')
it returns
trecord
-------
ALA0000
ALA0001
ALA0002
It means that I have records that starts with ALA and a digit after it,
EDIT
I'm doing it using PHP MySQL and innodb engine to be specific.
I think you can use REGEXP instead of LIKE
SELECT trecord FROM `tbl` WHERE (trecord REGEXP '^ALA[0-9]')
In my case (Oracle), it's WHERE REGEXP_LIKE(column, 'regex.*'). See here:
SQL Function
Description
REGEXP_LIKE
This function searches a character column for a pattern. Use this
function in the WHERE clause of a query to return rows matching the
regular expression you specify.
...
REGEXP_REPLACE
This function searches for a pattern in a character column and
replaces each occurrence of that pattern with the pattern you specify.
...
REGEXP_INSTR
This function searches a string for a given occurrence of a regular
expression pattern. You specify which occurrence you want to find and
the start position to search from. This function returns an integer
indicating the position in the string where the match is found.
...
REGEXP_SUBSTR
This function returns the actual substring matching the regular
expression pattern you specify.
(Of course, REGEXP_LIKE only matches queries containing the search string, so if you want a complete match, you'll have to use '^$' for a beginning (^) and end ($) match, e.g.: '^regex.*$'.)