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.
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 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;
I am trying to run a SELECT Query that pulls the Last Names that begin with T. When I run it I just get 0 results but there are names in the table that do begin with the letter T.
What am I doing wrong?
Here is my Code
SELECT FirstName, LastName, City
FROM Customers
WHERE LastName LIKE 'T%';
The most likely cause for this is that you have leading spaces in LastName column values.
To find all entries that have at least one leading space
SELECT FirstName, LastName, City
FROM Customers
WHERE LastName LIKE ' %';
Here is SQLFiddle demo
To fix such entries you can do
UPDATE Customers
SET LastName = TRIM(LastName)
WHERE LastName LIKE ' %';
Here is SQLFiddle demo
I want to know if there is any option to output multiple rows into a single row.
for example the regular select * from tbl_name will give a list of all records available in the table.
firstname lastname
---------- ------------
Lepanto Fernando
Lourdes Brillianto
Gerald Siluvai
Preferred output
firstname will have -> Lepanto###Lourdes###Gerald
lastname will have -> Fernando###Brillianto###Siluvai
Can we have some concatenation done to achieve the above.
Use GROUP_CONCAT()
select group_concat(firstname separator '###') as firstnames,
group_concat(lastname separator '###') as lastnames
from your_table
Use:
select GROUP_CONCAT(firstname SEPARATOR "###") as firstname,
GROUP_CONCAT(lastname SEPARATOR "###") as lastname
from tblname
I have a table with 3 fields, name, firstname and lastname
I want to see how many rows in the table have name of the form firstname%lastname
I tried to do
select * from family_watchdog_offender where name like firstname%lastname\G
but that returned a syntax error regarding the %lastname portion of the query. Is there some syntax that will allow me to run a query such as this?
SELECT * FROM family_watchdog_offender WHERE name LIKE CONCAT(firstname, '%', lastname);
Try concat-ing the %:
select * from family_watchdog_offender where name like CONCAT(firstname, '%', lastname)
I don't think you can do that...This may be what you need to do
SELECT *
FROM family_watchdog_offender
WHERE name LIKE CONCAT(firstname, '%')
AND name LIKE CONCAT('%', lastname);