Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Concatenating 2 fields when 1 is sometimes null Post Reply Post New Topic
Author Message
miamitourism
Newbie
Newbie
Avatar

Joined: 14 Jan 2011
Online Status: Offline
Posts: 24
Quote miamitourism Replybullet Topic: Concatenating 2 fields when 1 is sometimes null
     Posted: 23 Mar 2011 at 3:34am
Hello forum!
 
I am trying to seek a solution- forgive my igorance- I'm a newbie at this!
 
I am concatenating two fields from my database (address 1 and address 2) using a formula. What I am noticing, however, is that if the address 2 field is blank, the formula doesn't return anything. How can I instruct Crystal to concatenate address1 and address2, and if address 2 is blank, just return address 1?
 
Many thanks,
Sarah
IP IP Logged
Keikoku
Senior Member
Senior Member


Joined: 01 Dec 2010
Online Status: Offline
Posts: 386
Quote Keikoku Replybullet Posted: 23 Mar 2011 at 3:42am
local stringvar address := "";

address := address1;
if not isnull(address2) then
   address := address1 & address2;
address;
IP IP Logged
miamitourism
Newbie
Newbie
Avatar

Joined: 14 Jan 2011
Online Status: Offline
Posts: 24
Quote miamitourism Replybullet Posted: 23 Mar 2011 at 3:51am
WOW! It's so easy when you know how to do it!! Thank you so very much!

local stringvar address := "";

address := {cli_Clients.address1};
if not isnull({cli_Clients.address2})then
   address := {cli_Clients.address1} & {cli_Clients.address2};
address;

Worked perfectly!! Clap
IP IP Logged
miamitourism
Newbie
Newbie
Avatar

Joined: 14 Jan 2011
Online Status: Offline
Posts: 24
Quote miamitourism Replybullet Posted: 23 Mar 2011 at 3:56am

Would I be able to add a third field and accomplish the same thing? For example, I am doing the same with {cnt_contacts.prefix}, {cnt_contacts.firstname}, and {cnt_contacts.lastname}- but again, sometimes the prefix field is blank. 

IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 23 Mar 2011 at 4:00am

Crystal deals with Nulls in a slightly different way than you might expect. If you do not tell each formula what to do with Nulls or to 'use default values for Nulls' and it hits a Null on your row it will not evaluate the formula for that row and returns nothing.

You can write the formula to explicitly handle nulls as Keikoku has given you or you can set the formula to use 'default values for Nulls'.
This option should be in a drop down list in the formula workshop in the expression editor.
Also note, when dealing with Nulls in the written formula you have to write the Null portion as the first part of the statement.
This works to insert the word NULL for null values...
if isnull(field1) then 'This is NULL' else
if field1='A' then 'This is A' else
if field1<>'A' then 'This is not A'
 
This does not work to insert the word NULL for null values...
if field1='A' then 'This is A' else
if field1<>'A' then 'This is not A' else
if isnull(field1) then 'This is NULL'
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum