Find the string after a specific substring in mysql - mysql

I want to retrieve the last part of a large string in mysql after occurence of a particular substring. Please advice about how to do that.
For e.g. the main string contains
"New Delhi is capitalof India".
"Berlin is capitalof Germany".
I need to retrieve only India and Germany. I want know how to retrieve the data occuring after the specific substring like 'capitalof'.
I need to do this in mysql.

You can use substring_index
select
substring_index(title,'capitalof',-1)
from table1
DEMO

Related

How to make an dynamic SQL SELECT query by passing in a comma separated list of values?

I have a column in my SQL database (using mySQL) 'lastname', which has any number of employee's last names in it. I want to write a query that handles a search for last names using a comma delimited list.
So the user will type:
Garcia, Smith, Jones
And that gets stored in a String, lastNameQuery, which then gets passed into my backend API function that calls the SQL command. I want to return all employees in the DB that have those last names.
Is there any kind of SQL SELECT command I can use that search using a list like that? For my other functions (which only handle a single search term) I'm using this:
"SELECT * FROM employees WHERE salary LIKE '%${salary}%'"
Which works great. Is there some way I can modify it to handle a list? I can always break up the single String ("Garcia, Smith, Jones") into an array if necessary so that's not a problem. Any ideas?
You need to either do:
(lastname like '%Garcia%' or lastname like '%Smith%' or lastname like '%jones%')
or create a fulltext index on lastname (alter table employess add fulltext (lastname)) which would let you do
match (lastname) against ('Garcia, Smith, Jones')
but won't do things like find Garcia if you search for just "Garc".

SQL query to check whether the name is begin and ends with consonants

I am trying to retrieve the names from a student table starting and ending with consonants. I am trying to write
SELECT NAME
FROM STUDENT
WHERE NAME-LEFT(NAME,1) IN ('A','E','I','O','U')
AND NAME-RIGHT(NAME,1) IN ('A','E','I','O','U');
trying to omit the vowel's term, then the rest terms automatically become consonants, but I think there is an error in SQL query as I am also getting the terms that are started with vowels. How to fix this?
SELECT NAME
FROM STUDENT
WHERE LEFT(UPPER(NAME),1) NOT IN ('A','E','I','O','U') /*change done is to extract the first letter*/
AND RIGHT(UPPER(NAME),1) NOT IN ('A','E','I','O','U'); /*change done is to extract the last letter*/
Edit: from feedback of Akina, added the condition to cater to non vowels, and assumption made that the collation characteristics match

MySQL split string and get all substring except the last

I want to split the string in MySQL query using a delimiter. However, I want to split on the last occurrence of the string and get the first part of the string.
For example:-
'apple - banana - grape'
The result after splitting should be 'apple - banana'. The important thing is that we do not know how many occurrences of '-' will be there in the string.
On MySQL 8+, the REGEXP_REPLACE function works well for your requirement:
SELECT fruits, REGEXP_REPLACE(fruits, '\\s*-\\s*[^-]+$', '') AS fruits_out
FROM yourTable;
Demo
By the way, a much better table design would be to store each CSV fruit value in a separate row. This would alleviate the need to use regex to manipulate the list of fruits.

SQL - If input string is empty return all rows , otherwise try to match

I have table matches with these columns:
|sport|region|country|league
If the input string for sport is empty I want to return everything and don't bother with matching region, country etc.
If sport is not empty then find rows with matched sports and proceed to region and do the same thing.
Is this possible to do in SQL? I know I can filter this out in PHP and then run different SQL queries.
Try this
WHERE (sport_param IS NULL OR sport_column = sport_param)
You might want to use the LIKE operator or consider case-insensitive checking instead of simply comparing the exact sport.
Take a look at this
... you can basically make an if statement in SQL to match whether sport is empty or not and depending on that execute two different queries.

Selecting and compare one value with values in 2 columns of the DB

I have one value, that represents a zip code.
I need to see which city belongs to this zip code by comparing the given value with the values in two columns of the DB.
This is my _cities table:
city (name of the city, VARCHAR)
zipcode_start (the first zip code available, VARCHAR )
zipcode_end ( the last zip code available, VARCHAR ).
Where I leave zipcodes are numbered in sequence.
So if I have for example: city = Rome, zipcode_start = 00118 and zipcode_end = 00199 and the given zipcode is 00119, how do I get the city Rome from the DB?
00119 in this case is included in the sequence 00118 - 00199, so the DB should return Rome.
I can do this in many ways with PHP, but I am looking for an elegant way to do it directly with a SQL statement.
Is this possible?
Thanks for any help
I'd use BETWEEN, or if you don't like that, you can use >= and <=. And if you prefer joins over where clauses, you can even join on an inequality sometimes.
For example,
select city
from my_cities
where zipcode between zipcode_start and zipcode_end
You have to deal with getting the variable into that statement, but I get the impression that you can handle that part already. Is this what you are trying to do? I sort of doubt it, but that's all I get out of your question.