I started my first year of learnig software development and there is a question I just can't solve, and sadly because of the corona virus I can't ask a teacher util next week.
I have a table with full names and I need to separate them into a first name and a last name
I have found some solutions that use SUBSTRING_INDEX but I haven't red about it anywhere yet.
I somehow found a way to get the last name, but I just can't get the first name done.
This is how I did the last name:
SELECT RIGHT(name, LENGTH(name) - INSTR(name, " ")) AS lastname FROM student;
Is there anybody that can help me with this?
Thanks in advance
Use substring_index():
select substring_index(name, ' ', 1) as first_name,
substring_index(name, ' ', -1) as last_name
This assumes that the name has only two parts.
Use SUBSTRING_INDEX to get everything at the left of the first space as one value, and everything past the first space as other value:
SELECT substring_index(name, ' ', 1) AS firstname,
substring_index(name, ' ', -1) AS lastname
Related
select substring(custmer_name, 1, instr(custmer_name, ' ')) as first_name from sales.customers;
This solution is giving me the answer but It doesn't work with the last name
Please test this: I used locate function to define the position of ' '.
SELECT
LEFT(customer_name, LOCATE(' ',customer_name)-1) as first_name,
RIGHT(customer_name, LENGTH(customer_name)-LOCATE(' ',customer_name)) as last_name
FROM customer;
Result set:
Use SUBSTRING_INDEX() it takes 3 parameter:
Column name
Delimiter
Occurrence count
You can find more explanation in this article
Query
SELECT
SUBSTRING_INDEX(customer_name,' ', 1) as first_name,
SUBSTRING_INDEX(customer_name,' ', -1) as last_name FROM customer;
So I am trying to do this:
SELECT customerID, name, email
FROM Customers
WHERE customerID IN (substrings delineated by spaces of CID string)
Where what I want to happen, is it returns the entries that have a matching customerID.
Unfortunately, the value CID is a string of all the matching CIDs separated by spaces. I've tried to change it to be some sort of list, but no luck on that end. If I could use something like string splitter that would solve my problem, but it doesn't seem to be available in MySQL.
Sorry for the relatively simple question, but an hour of googling has returned no clear answers.
I wonder if you could use the REPLACE function:
SELECT customerID, name, email
FROM Customers
WHERE customerID IN (REPLACE(CID string,' ',','))
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_replace
One method is like:
SELECT customerID, name, email
FROM Customers
WHERE CONCAT(' ', CID, ' ') LIKE concat('% ', customerID, ' %') ;
I really do advocate that you separate the string. If it is coming from a table, then you probably need a junction/association table.
Try this:
WHERE FIND_IN_SET(CustomerID, REPLACE(spacedList, ' ', ',')) > 0
FIND_IN_SET
You might need to also cast CustomerID to a char, but I would try without the cast first.
I have a NAME column and the data is stored like this in a MySql database:
Doe,John P
I would like to break it out into FNAME LNAME MNAME and FULLNAME.
If I don't want to tamper with the SQL table, should I create a view?
I know how to fix it in PHP with a function but I would rather the processing be done on the database server than the application server.
This is what I have so far. I thinking I need a compound query or something similar.
SELECT Name
, SUBSTRING_INDEX(Name,',',-1) As FNAME
, SUBSTRING_INDEX(Name,',',1) As LNAME
, SUBSTRING_INDEX(Name,' ',1) As MNAME
FROM people;
I'm having trouble grabbing the middle name and doing what I think should be CONCAT function to get FULLNAME.
It would be great if there was a way (maybe a conditional) to handle names in the database that didn't have middle names. (i.e. DOE,JOHN).
A view is a fine way to get the results you want for future queries. The defining query would be:
SELECT Name, SUBSTRING_INDEX(SUBSTRING_INDEX(Name, ',', -1), ' ', 1) As FNAME,
SUBSTRING_INDEX(Name, ',', 1) As LNAME,
SUBSTRING_INDEX(Name, ' ', -1) As MNAME
FROM people;
This will work assuming that the format is exactly correct as you describe it in the table. Small changes -- such as no middle name or a space after the comma -- would make the syntax more complicated.
I have a table with:
Name Surname and Outputname ( = Name Surname )
Name and Surname should always have a value, but some time are empty.
I need to select from table by surname and i can't use surname field.
I can't even change field values with a script because i get this table from an outside source and can be change any time.
Database is MySQL 4.x
Can i select by second word in Outputname starting with some letter?
something like
SELECT Outputname FROM USERS
WHERE seconword(Outputname) LIKE 'A%' SORT BY seconword(Outputname) ASC
try this
SELECT
SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord
FROM USERS
WHERE SUBSTRING_INDEX(Outputname, ' ', -1) LIKE 'A%'
ORDER BY SecondWord ASC
demo
One possible approach:
SELECT Surname
FROM (
SELECT SUBSTRING_INDEX(Outputname, ' ', -1)
AS Surname
FROM Users) AS S
WHERE Surname LIKE 'A%'
ORDER BY Surname;
SQL Fiddle. This method is based on assumption that Outputname's format is always 'FirstName LastName' (i.e., ' ' symbol is used as a delimiter, and used only once each time).
I didn't understand your question at all. So you only have access to Outputname (composed by two words name+surname) and you have to sort it by the second word right?
Try something like (it worked for me):
SELECT
Outputname,
SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord
FROM USERS
ORDER BY SecondWord ASC
Clause SUBSTRING_INDEX(Outputname, ' ', -1) as SecondWord returns the last word placed after a space. So If you have Outputname = 'Maria Callas' it returns 'Callas', if you have Outputname = 'Sophia Cecilia Kalos' it returns 'Kalos'.
I was wondering if you have two columns for lets say
first name.
last name.
what you store in the database.
Can you create a 'dynamic' column 'fullname' in the database that automatically creates the name out of the first and last name?
firstname |lastname |fullname (this goes automatically)
-----------------------------------------
foo |bar |foo bar
If you just want to select the full name, you can do just that:
select concat(firstname, ' ', lastname) as fullname from users
If you actually want a column (i.e. be able to perform updates against it), then it's not possible, AFAIK.
You can try something like this:
SELECT CONCAT(firstname, ' ', lastname) as firstlast FROM users
This one also makes sure that no empty spaces are added:
SELECT IF(ISNULL(firstname), lastname, CONCAT(firstname, ' ', lastname)) AS fullname FROM users