I have the following sql questy
SELECT Name,Email
FROM customer
WHERE Email LIKE '%SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1)%'
OR Email LIKE '%SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1)%';
In the Name collonb I have names like "Gearge Martin" and i like to check if there is any similarity between the Names and Email addresses. But this query doesn't give any records back.
Any suggestions?
Thanks
Use WHERE Email LIKE CONCAT(...):
SELECT Name, Email
FROM customer
WHERE Email LIKE CONCAT('%', SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1), '%') OR
Email LIKE CONCAT('%', SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1), '%');
You were trying to compare the Email field against the literal string 'SUBSTRING_INDEX(...' which isn't what you want.
You are not including the column Name in your query. Change it as follows:
SELECT Name,Email
FROM customer
WHERE Email LIKE '%SUBSTRING_INDEX(SUBSTRING_INDEX(' + Name + ', ' ', 1), ' ', -1)%'
OR Email LIKE '%SUBSTRING_INDEX(SUBSTRING_INDEX(' + Name + ', ' ', 2), ' ', -1)%';
Related
I have a MYSQL database with one table named students.
The table has three fields namely, regNumber, fullName, and email.
The email field is currently empty for all records in the table.
I am able to generate email addresses based on the regNumber and fullName columns.
Below is the SQL script for achieving that:
SELECT
REPLACE(CONCAT(SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(fullName, ' ', 1), ' ', -1), 1, 1),
SUBSTRING_INDEX (SUBSTRING_INDEX(fullName, ' ', 3), ' ', -1),
SUBSTRING(regNumber, 7, 4), "#ut.edu.ua"), '`', '') AS student_email
FROM students;
However, I want to update the email field of every tupple with the result of executing the above script. How can I possibly achieve this?
update students
set email = REPLACE(CONCAT(SUBSTRING(SUBSTRING_INDEX(SUBSTRING_INDEX(fullName, ' ', 1), ' ', -1), 1, 1),
SUBSTRING_INDEX (SUBSTRING_INDEX(fullName, ' ', 3), ' ', -1),
SUBSTRING(regNumber, 7, 4), "#ut.edu.ua"), '`', '')
I wrote a query which is this:
SELECT mrt_name as MRT ,
operation_alpha_numeric_codes as Original,
SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', 1) as First_code,
SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -1) as Second_Code,
SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -2) as Third_Code
FROM scraping.xp_pn_mrt;
I got result like this
As you can see the second_code copies the value of the original or the first_code if the value doesn't have a corresponding space or data. Also, the third code gets the second_code in the records that have a third code in them. How do I prevent the data being copied or set it to blank when the code doesn't have a corresponding value in it and how can I achieve getting the third code without copying the second one? Can someone help me with my query and what's wrong with it? Thanks a lot.
Since you're using MariaDB, you can use REGEXP_REPLACE to extract the parts of the code that you want:
SELECT
operation_alpha_numeric_codes as Original,
REGEXP_REPLACE(operation_alpha_numeric_codes, '^([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?$', '\\1') as First_code,
REGEXP_REPLACE(operation_alpha_numeric_codes, '^([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?$', '\\2') as Second_code,
REGEXP_REPLACE(operation_alpha_numeric_codes, '^([^ ]+)(?: ([^ ]+))?(?: ([^ ]+))?$', '\\3') as Third_code
FROM data
Output for (part of) your sample data
Original First_code Second_code Third_code
NS23 NS23
NS24 NE6 CC1 NS24 NE6 CC1
NS25 EW13 NS25 EW13
Demo on dbfiddle
Here's a version that will also work on MySQL 5.7, using RLIKE to check if the input matches given patterns:
SELECT
operation_alpha_numeric_codes as Original,
SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', 1) AS First_code,
CASE WHEN operation_alpha_numeric_codes RLIKE '^([^ ]+)$' THEN ''
WHEN operation_alpha_numeric_codes RLIKE '^([^ ]+)( ([^ ]+))?$' THEN SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', -1)
ELSE SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', 2), ' ', -1)
END AS Second_code,
CASE WHEN operation_alpha_numeric_codes RLIKE '^([^ ]+)( ([^ ]+)){2}$' THEN SUBSTRING_INDEX(operation_alpha_numeric_codes, ' ', -1)
ELSE ''
END AS Third_code
FROM data
Demo on dbfiddle
You can try the following:
SELECT
mrt_name as MRT ,
operation_alpha_numeric_codes as Original,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 1), ' ', -1) AS First_code,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=1,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 2), ' ', -1) , '')
as Second_code,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=2,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 3), ' ', -1), '')
AS Third_code
FROM scraping.xp_pn_mrt;
[EDIT]
For double spaces between each value, This will work:
SELECT
mrt_name as MRT ,
operation_alpha_numeric_codes as Original,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=2,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 3), ' ', -1) , '')
as Second_code,
If( length(operation_alpha_numeric_codes ) - length(replace(operation_alpha_numeric_codes , ' ', ''))>=4,
SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes , ' ', 3), ' ', -1), '') AS Third_code
FROM scraping.xp_pn_mrt;
I have used CASE WHEN clause with LENGTH function.
I used LENGTH to calculate number of occurences of your separator ' ' in the string. CASE WHEN it is ONE occurances then there are TWO "results". CASE WHEN it is TWO occurances then there are THREE "results".
Here is the DEMO that will show the correct results for your two problematic data.
SELECT mrt_name as MRT
, operation_alpha_numeric_codes as Original
, SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', 1) as First_code
, CASE WHEN LENGTH(operation_alpha_numeric_codes) - LENGTH(REPLACE(operation_alpha_numeric_codes, ' ', '')) = 1
THEN SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -1)
WHEN LENGTH(operation_alpha_numeric_codes) - LENGTH(REPLACE(operation_alpha_numeric_codes, ' ', '')) = 2
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', 2), ' ', -1)
ELSE NULL
END Second_Code
, CASE WHEN LENGTH(operation_alpha_numeric_codes) - LENGTH(REPLACE(operation_alpha_numeric_codes, ' ', '')) = 2
THEN SUBSTRING_INDEX(SUBSTRING_INDEX(operation_alpha_numeric_codes,' ', -2), ' ', -1)
ELSE NULL
END Third_Code
FROM scraping.xp_pn_mrt;
I have a table in mysql -mytable.
Column Name:ResidenceAddress1
It has values like this:
sap3200#gmail.com,Rourkela
sap3212#gmail.com 2nd street,7 hils
2nd street, sap3212#gmail.com
I've tried this way:
select (case when substring_index(Residence_Address1, ' ', 1) like '%#%'
then substring_index(Residence_Address1, ' ', 1)
else substring_index(Residence_Address1, ' ', -1)
end) as email, Residence_Address1
from mytable
where Residence_Address1 like '%gmail%' and Email_Personal1=""
But its not giving me only email ids. How do I get only email ids from multiple words?
For your sample data, here is a solution:
select
concat(trim(substring_index(substring_index(ResidenceAddress1, '#', '1'), ' ', -1)), '#gmail.com') as mail
,ResidenceAddress1
from mytable
This answer is based on your email is all gmail.com and your separator is space.
Edited:
substring_index(ResidenceAddress1, '#', '1') will get you this:
sap3200
sap3212
2nd street, sap3212
then
substring_index('sap3200', ' ', -1) => sap3200
substring_index('sap3212', ' ', -1) => sap3212
substring_index('2nd street, sap3212', ' ', -1) => sap3212
Demo Here
I'm trying to split a name column (fullname) with the format lastname, firstname into two separate columns in MySQL using:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(fullname, ',', 1), ' ', -1) as firstname, SUBSTRING_INDEX(SUBSTRING_INDEX(fullname, ',', 2), ' ', -1) as lastname FROM tablename;
This works on most names. However, a name like "Del Torres Jr, Marcelo" shows up as
--------------------
lastname | firstname
--------------------
JR | Marcelo
--------------------
How to I need to alter my statement to capture all of a name after the comma?
Can you not just use:
SELECT SUBSTRING_INDEX(fullname, ',', 1) AS firstname
,SUBSTRING_INDEX(fullname, ',', -1) AS lastname
FROM table
SQLFiddle
I can't tell what you're trying to do with the match against ' ' but if you're just trying to trim whitespace you can use TRIM() on those values.
is this what you want ?
SELECT
SUBSTRING_INDEX(value, ' ,', 1) as lastname ,
SUBSTRING_INDEX(value, ' ,', -1) as firstname
FROM event;
demo
and in your query you should change -1 to 1 like that
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(fullname,',', 1),' ', -1) as firstname,
SUBSTRING_INDEX(SUBSTRING_INDEX(fullname,',', 2),' ', 1) as lastname
^--this should be 1
FROM tablename;
I am running the following query on a table, which splits the first name and last name of each person in the Name column of the table into Firstname and Lastname:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1) as Firstname,
SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1) as Lastname
FROM conference;
This works fine. I would now like to add the results of this to two new columns in the table which I have called Firstname and Lastname.
I tried adding INSERT conference [Firstname, Lastname] to the start of the Query, but that generated an error. Could someone help with the correct way of doing this?
Thanks,
Nick
If your intent is to update existing rows with those new fields instead of inserting new records, this should work
UPDATE Conference
SET
Firstname = SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1),
Lastname = SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1)
have you tried:
select SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 1), ' ', -1) as Firstname,
SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ' ', 2), ' ', -1) as Surname
into conference
from conference