Results 1 to 28 of 28

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

Hybrid View

  1. #1
    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

  2. #2
    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

  3. #3

    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.

  4. #4
    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

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