We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

simple Microsoft SQL question..   URGENT

meixell
meixell asked
on
Medium Priority
1,095 Views
Last Modified: 2008-02-01

I have a table with a first name and a last name column. What I want is to return only the last name if the first name is null, but (last name + ", " + first name) otherwise.

Any suggestions on how to do this without having to muck with a procedure? MySQL has a CONCAT_WS() (ws = with separator) function that does what I want, but I haven't been able to find any simple equivalent from Microsoft.
 
Comment
Watch Question

Top Expert 2006

Commented:
cant you just do

select lastName & coalesce(firstName & ",", "") from tableName
CERTIFIED EXPERT
Commented:
something like ...
SELECT
    CASE WHEN fname IS Null and lname is not null then rtrim(lname)
    WHEN fname IS not Null and lname is null then rtrim(fname)
    ELSE rtrim(lname) +','+rtrim(fname)
end
FROM mytable

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006

Commented:
or this may work
select lastName + coalesce(firstName & ', ', '') as pName from tableName
CERTIFIED EXPERT
Top Expert 2012

Commented:
This is a duplicate question that exceeds the maximum 500 allowed.   Please post a message in Community Support to have the points reducd in both threads as you have valid comments in both.  See here:
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_22035147.html
CERTIFIED EXPERT

Commented:
@meixell, please ask for a refund from CS of your points as this has already been answered in your other link.

Author

Commented:
Yea.. Sorry about the dual post. I didn't think my submission went through the first time.


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.