Results 1 to 28 of 28

Thread: [RESOLVED] SQL Server String Concatenation with Possible Nulls

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Resolved [RESOLVED] SQL Server String Concatenation with Possible Nulls

    Here's my query so far:

    Code:
    SELECT 
    	ISNULL(tblHealthCareProviders.HCPPrefix, '') + 
    	 tblHealthCareProviders.HCPFName + 
    	 ' ' +
    	 tblHealthCareProviders.HCPLName + 
    	 ISNULL(tblHealthCareProviders.HCPSuffix, ''), etc
    What I am doing is creating a single name field out of four possible data in a table: prefix (Dr., Mrs., etc) first name, last name and suffix (Jr., Sr., etc). This query is too simple because what I need to do is: if there is a suffix, append a comma and a space to last name and then append the suffix, otherwise just leave last name alone.

    So I would get John Berry, Jr. instead of John BerryJr which is what the query above is giving me.

    Do I have to break this out and code something more complex than ISNULL can handle?

    Thanks.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Server String Concatenation with Possible Nulls

    case....

    Code:
    select
      case when tblHealthCareProviders.HCPPrefix is not null then tblHealthCareProviders.HCPPrefix + ' ' else '' end + 
      tblHealthCareProviders.HCPFName + 
        ' ' +
      tblHealthCareProviders.HCPLName +
      case when tblHealthCareProviders.HCPSuffix is not null then ', ' + tblHealthCareProviders.HCPSuffix else '' end as FULLNAME
    ....
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    I've done this in a UDF

    Code:
    CREATE FUNCTION dbo.GetFullName_F (@LastName as varchar(35),@FirstName as varchar(20),@MiddleName as varchar(20),@Suffix as varchar(10),@Opt as int)
    RETURNS varchar(100) AS  
    BEGIN
    	Declare @WN varchar(100)
    
    	Set @WN=IsNull(@LastName,'')+', '+IsNull(@FirstName,'')
    		+Case When IsNull(@MiddleName,'')<>'' Then ' '+@MiddleName Else '' End
    		+Case When IsNull(@Suffix,'')<>'' Then ', '+@Suffix Else '' End
    
    	RETURN @WN
    END

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Server String Concatenation with Possible Nulls

    Only thing missing is the title/prefix, but that's easily added in...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    This won't work

    tblHealthCareProviders.HCPSuffix is not null

    if the field contains blanks...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    Quote Originally Posted by szlamany View Post
    This won't work

    tblHealthCareProviders.HCPSuffix is not null

    if the field contains blanks...
    I cannot test that as the field does not contain blanks, but that does not mean it won't. So thanks for catching that.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    I only use FIELD IS NULL or FIELD IS NOT NULL when testing for date columns

    Otherwise I feel safer using

    IsNull(FIELD,'')<>''

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    Before I saw szlamany's post, I had worked up to this:

    Code:
    SELECT Name = 
    	CASE  
    		WHEN HCPSuffix IS NULL OR HCPSuffix = '' THEN
    			ISNULL(tblHealthCareProviders.HCPPrefix, '') + 
    				 tblHealthCareProviders.HCPFName + 
    				 ' ' +
    				 tblHealthCareProviders.HCPLName 
    		ELSE
    			ISNULL(tblHealthCareProviders.HCPPrefix, '') + 
    				 tblHealthCareProviders.HCPFName + 
    				 ' ' +
    				 tblHealthCareProviders.HCPLName + 
    				 ', ' +  tblHealthCareProviders.HCPSuffix
    		END
    FROM tblHealthCareProviders
    to take care of blanks in the suffix field.

    I should modify this to IsNull(FIELD,'')<>" - yes?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Server String Concatenation with Possible Nulls

    depends on the context... I have several places where blank and NULL are two very distinctly different meanings ... NULL being what it is supposed to mean: unknown... while a blank value is a valid value and needs to be treated as such.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    Quote Originally Posted by MMock View Post
    ...I should modify this to IsNull(FIELD,'')<>" - yes?
    I would for this specific situation...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    But in this case, blank and NULL I believe would both signify "there is no suffix". And like I said, right now suffix is never blank. and I just saw that there's close to 500,000 rows in the table. That's a pretty good sampling!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    I am a bit of a purist - and I just don't like this code

    HCPSuffix IS NULL OR HCPSuffix = ''

    for one single reason.

    If a field can possibly contain nulls then it should never - imo - be used in an = or <> situation without IsNull() function.

    Null can NEVER equal anything.

    Granted your OR statement protects you - but I still can't get around the invalid statement of HCPSuffix = ''.

    What if someone comes along later and makes the FORMULA more complex - adding some more OR's - or simply removes the HCPSuffix is null?

    The only reason the HCPSuffix = '' works is because you have "sister-code" sitting next to it protecting it from itself.

    My two cents!

    btw - you still working in CT?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    I appreciate your insistence. And the fact is, we are writing this code for a client and then they are buying the source for maintenance on their own (not usually how we do things here) so it really should be perfect.

    Yes - Stratford.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    Shelton - wow - small world - and I do health care software for insurance companies - how coincidental!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    That is funny! Before, I was in West Hartford then Bloomfield. I've been here a year and it's permanent so I hope to stay a long time. This just happens to be healthcare - TLBB. We do a variety of sites, like Hilton Hotels, Owens Corning, Invisalign (braces), etc.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  16. #16
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    Health care is just a small part of what I do - but mostly work for not-for-profit like firms - unions, school districts - towns and such.

    TLBB? bb as in blue cross/blue shield?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  17. #17

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    "The Little Blue Book". It's a directory of practices and providers. I can't remember where the client is, but they are in CT too.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  18. #18

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    Now I've probably said too much and will get fired for breach of confidentiality...
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  19. #19
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: SQL Server String Concatenation with Possible Nulls

    Remove it if you feel you need to - the google spiders will find it in the next few minutes!!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  20. #20

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    Are you joking? Because I was. But I am learning to be paranoid and am getting better at it. How do you remove a post?
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  21. #21

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: SQL Server String Concatenation with Possible Nulls

    Actually, I just googled it, and TLBB is an acronym for many things, and The Little Blue Book also has several definitions. Wikipedia has a completely different description of it.

    Anyway, this is what I ended up with:
    Code:
    SELECT 
    
      CASE 
      -- Build "Name" out of the prefix, first name, last name and suffix, checking for nulls in the prefix and suffix parts. 
      
        -- Note, this: IsNull(FIELD,'')<>'' is a safer way of checking for nulls in FIELD than a direct ISNULL test.
    	WHEN ISNULL(tblHealthCareProviders.HCPPrefix,'')<>'' THEN 
    		tblHealthCareProviders.HCPPrefix + ' ' 
    	ELSE '' 
    	
    	END 
    	
    	+ tblHealthCareProviders.HCPFName 
    	+ ' ' 
    	+  tblHealthCareProviders.HCPLName 
    	+
      
    	  CASE 
    		WHEN ISNULL(tblHealthCareProviders.HCPSuffix,'')<>'' THEN 
    			', ' + tblHealthCareProviders.HCPSuffix 
    		ELSE '' 
    		END 
    				
    	AS Name, 
    etc (omitted, to protect the innocent)
    Thanks.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  22. #22
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls

    "but mostly work for not-for-profit like firms " - Crap! Srsly? I need to stop helping you then.... that's our target too...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls

    @tg - I tried to PM you - I guess u don't take them !

    PM me with "What company do you work for?"

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  24. #24

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls

    You guys - whatever happened to good, old-fashioned email???
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  25. #25
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls

    I got 143 spam emails yesterday alone - these are ones that my COMCAST BUSINESS CLASS provider and OUTLOOK didn't already put to junk mail...

    I try to limit the use of my email as much as possible - just to my actual clients!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  26. #26

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls

    Oh, I see.

    You can use the VBForums mail. That's what I use to email other members here.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  27. #27
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls

    Wow - I thought PM was the only forum method - didn't really know about vbforum email...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  28. #28

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Re: [RESOLVED] SQL Server String Concatenation with Possible Nulls

    Ha - I just sent you one.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width