How do i get the first name from the customer_name column - mysql

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;

Related

seperate full name into first name and last name MySQL

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

I want to put 'given' and 'surname' column together into 'fullname' in SQL, how would i go about in doing so. tnx

I currently have them separated but would prefer to have both together in one column.
SELECT CONCAT(given, ' ', surname) AS fullname FROM tablename
or for null safe,
SELECT CONCAT_WS(' ', given, surname) AS fullname FROM tablename
you can concatenate them using:
select CONCAT(given,' ',surname) as fullname;

Concat 2 columns in a full table view

here is an example of what my problem is
CustomerID FirstName LastName FullName
1 Ken Weger Ken Weger
this is my output what i need to do is to replace the FirstName and LastName columns with FullName using Concat in a select statement. The code I am getting to create this result looks like this
Select *,
Concat (firstname, ' ', lastname) as fullname from customer;
Please let me know what i am doing wrong and how to fix it.
SELECT CustomerID, CONCAT(FirstName, ' ', LastName) AS FullName FROM Table
select * selects all columns from table.

MYSQL extract uppercase words

I am trying to extract words which are in uppercase and lowercase.
Example, in single column(Full_name) having first and last name values, here I need to separate them as first and last names, first names always have in initial capital letter and last names have always all letters in uppercase.
Full_Name:
---------
abc ABC
pqr RTF
now I need separate them in 2 different columns as below
First_name Last_name
--------- --------
abc ABC
pqr RTF
Thank you very much for you inputs in advance.
SELECT LEFT(Full_Name,LOCATE(' ',Full_Name)) AS First_name,
SUBSTRING(Full_Name,LOCATE(' ',Full_Name)) AS Last_name
FROM TABLE_NAME
Check SQL Fiddle
The easiest way in MySQL is to use substring_index():
select substring_index(full_name, ' ', 1) as First_Name,
substring_index(full_name, ' ', -1) as Last_Name
from table t;
EDIT:
I missed the names with multiple parts.
select substring_index(full_name, ' ', 1) as First_Name,
trim(substring(full_name,
length(substring_index(full_name, ' ', 1))
)
) as Last_Name
from table t;

mysql query where second words starts with a letter

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