 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 12th, 2008, 05:47 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 62
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Concatenating Columns in SELECT Problem
Ok, I am bothered that I don't know the answer to this - but here is my dilema.
I am trying to combine last name, first name, and middle initial into a column named "Customer Name" in my query. Here is the query in question:
SELECT [customer_record], [mail_date], ([last_name] + ', ' + [first_name] + ' ' + [middle_init]) as [Customer Name], [email], [phone] FROM [customers] WHERE ([dealer_code] = @dealer_code) ORDER BY [mail_date]
The issue I am having is that when the middile initial is NULL, which is often - the entire Customer Name column is null, even though there are values in the first and last name.
Any ideas? (I know I am going to kick myself as soon as I find the answer)
|
|

June 12th, 2008, 05:59 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
Good old CASE WHEN:
Code:
SELECT customer_record, mail_date,
(last_name + ', ' + first_name + CASE WHEN middle_init IS NULL THEN '' ELSE ' ' + middle_init END) as Customer Name,
email, phone
FROM customers WHERE dealer_code = @dealer_code
ORDER BY mail_date
I got rid of your extraneous ( ) and [ ] ... no reason for any of the ones I omitted.
|
|

June 12th, 2008, 06:41 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 62
Thanks: 0
Thanked 1 Time in 1 Post
|
|
oh good lord - thanks!!!
I normally don't use the brackets but I copies this directly from a .net web form and that is how it was generated.
Appreciate it as always!
|
|

June 12th, 2008, 07:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
You know, if you don't care about an unnecessary trailing space, you can do this even easier:
Code:
SELECT customer_record, mail_date,
(last_name + ', ' + first_name + ' ' + ISNULL(middle_init,'') ) as Customer Name,
email, phone
FROM customers WHERE dealer_code = @dealer_code
ORDER BY mail_date
So if middle_init is indeed null, then you'll have a trailing space. But if you are showing this in typical HTML display, or printing it on paper, nobody can see the space, so what does it matter?
|
|

June 12th, 2008, 07:23 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
No need for trailing space... anything plus NULL is still NULL...
SELECT customer_record, mail_date,
(last_name + ', ' + first_name + ISNULL(' ' + middle_init,'') ) as Customer Name,
email, phone
FROM customers WHERE dealer_code = @dealer_code
ORDER BY mail_date
--Jeff Moden
|
|

June 13th, 2008, 01:08 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
|
|
DOH! OF COURSE!
I sure overcomplicated it!
Funny part is that I have preached on NULL not being same as EQUAL or NOT-EQUAL many times. Why didn't I tumble to adding to null being same? 20 lashes with a wet SQL Server manual for me!
|
|

June 13th, 2008, 02:50 PM
|
|
Friend of Wrox
|
|
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
|
|
Heh... no problem... I've made the same mistake a couple of times.
Everyone has to remember... NULL is NOT Nothing! :)
--Jeff Moden
|
|
 |