• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1070
  • Last Modified:

simple Microsoft SQL question.. URGENT

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.
1 Solution
cant you just do

select lastName & coalesce(firstName & ",", "") from tableName
something like ...
    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)
FROM mytable
or this may work
select lastName + coalesce(firstName & ', ', '') as pName from tableName
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Anthony PerkinsCommented:
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:
@meixell, please ask for a refund from CS of your points as this has already been answered in your other link.
meixellAuthor Commented:
Yea.. Sorry about the dual post. I didn't think my submission went through the first time.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now