Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language 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
  #1 (permalink)  
Old May 10th, 2004, 01:38 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Convert NULL to String for Concatenation?

I have a database containing a nmae broken down into constituent parts. I would like to string them together for a view as "Lastname, Firstname I." Unfortuantely, middle initial is not required and the NULL results in NOTHING appearing in this column of my view (the name looks fine as long as the field is not NULL).

How can I convert a NULL database field to a SPACE so that this error does not occur?
  #2 (permalink)  
Old May 10th, 2004, 02:01 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Ron,

You can take a look at the coalesce function. This functions returns the first none null element you pass to it. Since adding a space to null still results in null, you could do it like this:

SELECT FirstName + coalesce(' ' + Initials + ' ', ' ') + LastName AS FullName FROM Users

This will test Initials. When it's not null, a space followed by the initials followed by another space is returned. If the initials are null, just a single space (to separate the first name from the last name) is returned.
You can change the stuff you pass to coalesce if other fields can be null as well. In that case, you don't want the initials to be responsible for adding the spaces.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Thirty-Three by Smashing Pumpkins (Track 3 from the album: Mellon Collie and the Infinite Sadness - Twilight to Starlight) What's This?
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (February 22nd, 2013)
  #3 (permalink)  
Old May 10th, 2004, 02:12 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

I figured there'd be something that would do this, but COALESCE wasn't the first search term that leapt to mind. Tried and worked as advertised. Thanks, Imar!
  #4 (permalink)  
Old May 10th, 2004, 02:21 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yeah, odd name, isn't it. You'd expect something like IsNull. But maybe that was too obvious ;)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Dreamland by The B-52's (Track 5 from the album: Good Stuff) What's This?
  #5 (permalink)  
Old September 22nd, 2006, 04:59 PM
Authorized User
 
Join Date: Apr 2006
Location: Stockholm, , Sweden.
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey,
I don't even know how to pronounce it!

Thnx for the tip. (You're everywhere, Imar!)
Pettrer

Quote:
quote:Originally posted by Imar
 Yeah, odd name, isn't it. You'd expect something like IsNull. But maybe that was too obvious ;)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Dreamland by The B-52's (Track 5 from the album: Good Stuff) What's This?
Coding is indeed a nine-to-five job; nine pm to five am.
  #6 (permalink)  
Old September 23rd, 2006, 05:54 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

;)

You're welcome....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance in String concatenation rushman XSLT 4 September 27th, 2007 11:02 AM
String concatenation query. AjayLuthria XSLT 2 March 14th, 2007 11:55 AM
string concatenation nulogix PHP How-To 1 June 24th, 2004 06:17 AM
string concatenation matt_99 Access VBA 2 January 17th, 2004 09:55 AM
T-SQL String Concatenation jaucourt SQL Language 2 January 13th, 2004 10:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.