Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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?
Reply With Quote
  #2 (permalink)  
Old May 10th, 2004, 02:01 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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?
Reply With Quote
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!
Reply With Quote
  #4 (permalink)  
Old May 10th, 2004, 02:21 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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?
Reply With Quote
  #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.
Reply With Quote
  #6 (permalink)  
Old September 23rd, 2006, 05:54 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:43 AM.


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