CONCAT names with condition
-
Hi, I have first_name, middle_name and last_name in my customers table. I want to do
SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)
The middle name is optional so I want to ignore it if it's blank to be:
SELECT CONCAT(first_name, ' ', last_name)
because if I do it the normal way I'll have extra space between the first_name and last_name
Technology News @ www.JassimRahma.com
-
Hi, I have first_name, middle_name and last_name in my customers table. I want to do
SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)
The middle name is optional so I want to ignore it if it's blank to be:
SELECT CONCAT(first_name, ' ', last_name)
because if I do it the normal way I'll have extra space between the first_name and last_name
Technology News @ www.JassimRahma.com
You can use ISNULL on the first middle name or a case statement. Case statement is more robust as it caters for ''.
Never underestimate the power of human stupidity RAH
-
Hi, I have first_name, middle_name and last_name in my customers table. I want to do
SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)
The middle name is optional so I want to ignore it if it's blank to be:
SELECT CONCAT(first_name, ' ', last_name)
because if I do it the normal way I'll have extra space between the first_name and last_name
Technology News @ www.JassimRahma.com
As long as you're doing it that way, you might as well use REPLACE. :-D
You'll never get very far if all you do is follow instructions.
-
Hi, I have first_name, middle_name and last_name in my customers table. I want to do
SELECT CONCAT(first_name, ' ', middle_name, ' ', last_name)
The middle name is optional so I want to ignore it if it's blank to be:
SELECT CONCAT(first_name, ' ', last_name)
because if I do it the normal way I'll have extra space between the first_name and last_name
Technology News @ www.JassimRahma.com
Without using
CONCAT
you can do this:rtrim(isnull(first_name,'',first_name+' ') + isnull(middle_name,'',middle_name+' ') + isnull(last_name,'',last_name))
The
rtrim
will remove a trailing space where the person does not have a last name.“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens