Solved

SQL - how to conditional add space when append a string

Posted on 2012-04-04
2
448 Views
Last Modified: 2012-04-04
I have
a.[Name] + ' ' + a.[MiddleName] + ' ' + a.[SurName] As Name

This will create a string with one spacing between words

However when a.[MiddleName] is empty, I will have two spaces between Name and SurName.  How can I structure the above so there will be only a single space if MiddleName is empty
0
Comment
Question by:tommym121
2 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 37806430
Please try:

a.[Name] + ' ' + (case when a.[MiddleName] = '' then '' else a.[MiddleName] + ' ' end) + a.[SurName] As Name
0
 

Author Closing Comment

by:tommym121
ID: 37806461
Thanks.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

705 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

15 Experts available now in Live!

Get 1:1 Help Now