Important alert: (current site time 7/15/2013 7:51:13 PM EDT)
 

VB icon

Concatenate address fields for display

Email
Submitted on: 9/13/2006 8:10:11 AM
By: Jonathan Spinks 
Level: Beginner
User Rating: Unrated
Compatibility: SQL Server 2000
Views: 10393
author picture
 
     When displaying address information from multiple fields in your database generally each field is separated by a carriage return line feed. If one of the values is null or an empty field then you don’t want to display the empty lines. This little function ignores empty lines but adds the CRLF on to other address lines.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Concatenate address fields for display
-- Description:When displaying address information from multiple fields in your database generally each field is separated by a carriage return line feed. If one of the values is null or an empty field then you don’t want to display the empty lines. This little function ignores empty lines but adds the CRLF on to other address lines.
-- By: Jonathan Spinks
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1096&lngWId=5--for details.--**************************************

IF EXISTS (select * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[fnAddCR]') AND xtype in (N'FN', N'IF', N'TF'))
	DROP function [dbo].[fnAddCR]
GO
create function dbo.fnAddCR(@sIn varchar(150)) 
	returns varchar(150) 
/*
Developer:	Jonathan Spinks
Date: 13/09/06
Description: Concatenates address fields with CRLF's for display purposes
Inputs: 
	@sIn = the string that if not null or empty an CRLF is added to
Return:
	the input string with added CRLF or an empty string (note nulls are converted into empty strings)
Example usage:
Select dbo.fnAddCR('Address1')+dbo.fnAddCR('Address2')+dbo.fnAddCR('Address3')+dbo.fnAddCR('City')+dbo.fnAddCR('County')
returns:
Address1
Address2
Address3
City
County
Select dbo.fnAddCR('Address1')+dbo.fnAddCR('')+dbo.fnAddCR('Address3')+dbo.fnAddCR(null)+dbo.fnAddCR('County')
returns:
Address1
Address3
County
Copyright © 2006 Programming. Heathmill
*/
 
Begin
	return isnull(nullif(@sIn+char(13)+char(10),char(13)+char(10)),'')
end
GO


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/15/2006 8:38:18 AMJeff Moden


Good idea. Just a hint, though... Because NULL+anything = NULL (unless you've deviated from the normal database settings), the following will perform the same as the code in your function...

ISNULL(@sIn+CHAR(13)+CHAR(10),'')

In other words, you don't need the NULLIF.


(If this comment was disrespectful, please report it.)

 
9/28/2006 4:51:48 AMvijay singh rana

Nice
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.