Solved

simple Microsoft SQL question..   URGENT

Posted on 2006-10-23
8
1,059 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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…

839 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