Solved

Simple SQL question.. URGENT!

Posted on 2006-10-23
4
1,171 Views
Last Modified: 2012-06-27

 
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
4 Comments
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17793339
SELECT LastName + ISNULL(', ' + FirstName, '') As Name
FROM ...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 17793369
This is a duplicate question that exceeds the maximum 500 allowed. Please post a message in Community Support to have the points reduced in both threads as you have valid comments in both.  See here:
http://www.experts-exchange.com/Databases/Q_22035145.html
0
 
LVL 33

Accepted Solution

by:
sajuks earned 500 total points
ID: 17793996
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 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