Ryan
Ryan

Reputation: 217

MySQL string and concat

So I want to take the first letter of the first name and all of the last name, add them together and make that the person's username.

I was using CONCAT() and I keep getting an unexplained syntax error from MySQL.

I have:

SELECT 
CONCAT(left(first_name, 1), left(last_name)) username
FROM survey_responders;

And all I get is "Syntax Error" as my error. What am I doing wrong?

Upvotes: 0

Views: 54

Answers (2)

Ola
Ola

Reputation: 68

The function left is good but substr is more efficient, try this instead:

select concat(substr(first_name, 1, 1), last_name) username FROM survey_responders;

Upvotes: 0

Dan
Dan

Reputation: 11084

You are missing the second argument for left(last_name), but I don't even think you need that call to left since you want the whole thing. So:

SELECT 
CONCAT(left(first_name, 1), last_name) username
FROM survey_responders;

Upvotes: 1

Related Questions