Solved

simple Microsoft SQL question..   URGENT

Posted on 2006-10-23
8
1,058 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

832 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