I'm trying to get employee's full name and combine them using MySQL function "Concat". Some of our employee don't have middle name and in this case SQL throws an error. How can i get full name of an employee even if the employee doesn't have middle initial.
SELECT CONCAT(`Employee`.`F_NAME`,
' ',
LEFT(`Employee`.`M_NAME`, 1),
'. ',
`Employee`.`L_NAME`)
FROM `Employee`
Try to use IFNULL
SELECT CONCAT(`Employee`.`F_NAME`,
' ',
IFNULL(CONCAT(LEFT(`Employee`.`M_NAME`, 1),'. '),''),
`Employee`.`L_NAME`)
FROM `Employee`
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;
I have a problem joining tables in the result column. i have a working query which combine different tables using UNION but when i'm extending another table i got an error saying 'The used SELECT statements have a different number of columns'
this is my query:
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(EmDesignation, ', ', Department) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblemployee as e, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
e.RFIDNo LIKE '6424823943')
UNION
(SELECT
IDNumber,
CONCAT(LastName, ', ', FirstName, ' ', Middle) as Name,
CONCAT(Course, ', ', Year) as Information,
Image,
v.PlateNo as PlateNumber
FROM
tblstudents as s, tblvehicle as v
WHERE
v.RFIDNo LIKE '6424823943'
AND
s.RFIDNo LIKE '6424823943')
I have problem with this. Continuation query above
UNION
(SELECT
Barrier
FROM
tblinformation as inf
WHERE
inf.RFIDNo IN (6424823943)
ORDER BY
AttendanceNo DESC LIMIT 1)
The error message is correct. Add NULLs to your second query to match the column number, and it will work.
For example:
SELECT
Barrier,
NULL,
NULL,
NULL,
NULL
FROM
tblinformation as inf
...
The error message states what the problem is. Just improve number of columns in SELECT and it will work correctly.
I have a data that have this result:
I'm using the query
select substring_index(descriptn, ' ', -1) from table1
and I get my result along with the dash for example "JACQ-ARMIE"
I want to get only JACQ and ABBY. Can you give me hints on how to get the name? Does this also apply for substring_index?
Just use substring_index() again:
select substring_index(substring_index(descriptn, ' ', -1), '-', 1)
from table1;
I'm trying to create a view using sqlfiddle v 5.6 or MySQL workbench v5.7 to include all information from a table CUSTOMERS, but concatenating firstName and lastName as wholeName.
I've tried using the following:
CREATE VIEW v_customer AS SELECT *,
CONCAT(CONCAT(lastName, ', '), firstName AS wholeName,
FROM customers;
and
CREATE VIEW v_customer AS SELECT customerID,
CONCAT(CONCAT(lastName, ', '), firstName AS wholeName,
...(all other customer columns),
FROM customers;
When leaving out the CONCAT function, the view is created. It leads me to believe there is something wrong with my syntax, but the error is brought up at the "FROM" line.
You can use single concat to concat more than two column or expressions together.
Try this:
create view v_customer as
select *,
concat(lastname, ', ', firstname) as wholename,
from customers;
You shoudl concat one tame only adding all the string an separatore you need
CREATE VIEW v_customer AS SELECT *,
CONCAT(lastName, ', ', firstName ) AS wholeName,
FROM customers;
The code that eventually worked for me, prior to receiving these answers...which also worked, was:
CREATE VIEW v_customer
AS SELECT customerID,
CONCAT(customers.firstName, ' ',customers.lastName) AS wholeName,
street,
apartment,
city,
state,
zipCode,
homePhone,
mobilePhone,
otherPhone
FROM CUSTOMERS;
I have a mysql table where i want to delete the first word of a filed.
For example table have a column "product name". which contains values -
Dabur Honey
haldiram Namkeen
Colgate Toothpaste etc.
I want to delete Dabur, haldiram, Colgate and make it.
Honey
Namkeen
Toothpaste
is that possible to do this using mysql query? How can i do that?
Devesh
That will be
UPDATE t SET product_name=SUBSTRING(product_name, LOCATE(' ', product_name))
SELECT SUBSTRING_INDEX(field, ' ', -1) FROM table
It'll take everything after last space.
select substring(`product name`, instr(`product name`, ' ')) as new_prod_name
from your_table
or if you like to update the table
update your_table
set `product name` = substring(`product name`, instr(`product name`, ' '))
SQLFiddle demo