Solved

simple Microsoft SQL question..   URGENT

Posted on 2006-10-23
8
1,056 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.
 
0
Comment
Question by:meixell
8 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17793278
cant you just do

select lastName & coalesce(firstName & ",", "") from tableName
0
 
LVL 33

Accepted Solution

by:
sajuks earned 20 total points
ID: 17793288
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
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17793359
or this may work
select lastName + coalesce(firstName & ', ', '') as pName from tableName
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17793368
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
0
 
LVL 33

Expert Comment

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

Author Comment

by:meixell
ID: 17802154
Yea.. Sorry about the dual post. I didn't think my submission went through the first time.


0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now