Jump to content
We've recently updated our Privacy Statement, available here ×

Concatenating 2 fields, one of them null


pennywiser

Recommended Posts

Hi! I'm trying to concatenate 2 fields in a single box. The box have the "Blank when null" property checked, the problem is that when the box receive any of the fields with data, and the second field in "null" it shows the null text instead the blank space.

I though about fixing it by Java code, changing the null for a blank space like " " but I would like to know if there's a way to do it with iReport 1.2.5.

Thanks!

Code:
$F{codigoRecomendaciones}+"  -  "+$F{ordenRecomendaciones} 
Link to comment
Share on other sites

  • Replies 12
  • Created
  • Last Reply

Top Posters In This Topic

You should be able to create a variable and test for either item being null there. Something like this should work:

$F{codigoRecomendaciones}!=null&$F{ordenRecomendaciones}!=null?$F{codigoRecomendaciones}+"  -  "+$F{ordenRecomendaciones}:F{codigoRecomendaciones}==null?$F{ordenRecomendaciones}:F{codigoRecomendaciones}I have never used that version of iReport but have done this type of thing in later versions so it should work. 

 

Link to comment
Share on other sites

  • 1 year later...

the best way is doing it with java:

($F{field_1}!=null?$F{field_1}:"")+($F{field_2}!=null?$F{field_2}:"")+...($F{field_8}!=null?$F{field_8}:"")

another way is to use DB function...

in Oracle try nvl:

select ...
   nvl(field_1,' '),
   nvl(field_2,' '),
   nvl(field_3,' '),
    ...
   nvl(field_4,' '),
   ...
from aTable



_________________________________________

if it works... give me KARMA points please!    : ) 
_________________________________________

listening:
Placebo - Where is my mind

Link to comment
Share on other sites

Thanks Slow,

 

I am still having a bit of a problem, if I insert a break between each field ($F{field_1}!=null?$F{field_1}:"")+($F{field_2}!=null? ", ":$F{field_2})

 

so when there is more than one field it grows. IE: field 1, field 2, field 3, field 4 etc but when there is just one it reads field 1. I get "," when the field is null and have "null" appearing again.

 

Many Thanks for your help,

Link to comment
Share on other sites

I think you're committing something wrong with java String concatenation...

try this:

(($F{field_1}!=null?$F{field_1}:"")+($F{field_2}!=null?","+$F{field_2}:"")+($F{field_3}!=null?","+$F{field_3}:"")+($F{field_4}!=null?","+$F{field_4}:""))

 

if you want delete the first "," if it apper you can use:

(($F{field_1}!=null?$F{field_1}:"")+($F{field_2}!=null?","+$F{field_2}:"")+($F{field_3}!=null?","+$F{field_3}:"")+($F{field_4}!=null?","+$F{field_4}:"")).startWith(",")?(($F{field_1}!=null?$F{field_1}:"")+($F{field_2}!=null?","+$F{field_2}:"")+($F{field_3}!=null?","+$F{field_3}:"")+($F{field_4}!=null?","+$F{field_4}:"")).substring(1):(($F{field_1}!=null?$F{field_1}:"")+($F{field_2}!=null?","+$F{field_2}:"")+($F{field_3}!=null?","+$F{field_3}:"")+($F{field_4}!=null?","+$F{field_4}:""))

or you can use a variable the simplify this notation...

 

_________________________________________

if it works... give me KARMA points please!    : ) 
_________________________________________

listening:
Underworld - Juanita/Kiteless [Everything, Everything]

 

 

oh... :)

I'm posting this and in the while you have posted a reply to... :)
well :)

 

 



Post Edited by slow at 05/27/2010 09:53
Link to comment
Share on other sites

using variables:

define a variable

temp = (($F{field_1}!=null?$F{field_1}:"")+($F{field_2}!=null?","+$F{field_2}:"")+($F{field_3}!=null?","+$F{field_3}:"")+($F{field_4}!=null?","+$F{field_4}:"").........)

then in the textfield:

$V{temp}.startsWith(",")?$V{temp}.substring(1):$V{temp}

 

thanks for karma... :)

 

_________________________________________

if it works... give me KARMA points please!    : ) 
_________________________________________

listening:
Underworld - Juanita/Kiteless [Everything, Everything]



Post Edited by slow at 05/27/2010 10:13
Link to comment
Share on other sites

  • 2 years later...

Hi,

 

I hope it is OK bringing this old post back to life!

 

I wanting to format a report to present data prefixed with text if the field was filled in the db but not if left null.

 

I am using the following code

(($F{old_tag1}!=null?"Tag Number: "+$F{old_tag1}+". ":"")+($F{tpo}!=null?" TPO Number: "+$F{tpo}+". ":" ")+
($F{height}!=null?"Height(m): "+$F{height} + ". ":"")+($F{spread}!=null?"Spread: "+$F{spread} +"m. ":"")+($F{distance_road}!=null?"Distance Nearest Structure:  "+$F{distance_road} + "m. ":"")+
($F{dbh}!=null?"DBH(cm): "+$F{dbh}:"")+($F{dbh_2}!=null?", "+$F{dbh_2}:"")+($F{dbh_3}!=null?", "+$F{dbh_3}:"")+($F{dbh_4}!=null?", "+$F{dbh_4}+". ":"")+
($F{number_of_stems}!=null?"Number of Stems: "+$F{number_of_stems} + ". ":"")+($F{age_class_description}!=null?" Life Stage: "+$F{age_class_description}+ ". ":"")+($F{vitality_description}!=null?"Vitality: "+$F{vitality_description}+ ". ":"")+
($F{park_value_description}!=null?"Physiological Condition: "+$F{stability_description}+". ":"")+($F{stability_description}!=null?"Structural Condition: "+$F{stability_description}+". ":"")+
($F{longevity_description}!=null?"Useful life expectancy(years): "+$F{longevity_description}+". ":"")+($F{longevity_after_description}!=null?"Useful life expectancy after work (years): "+$F{longevity_after_description}+". ":"")+($F{note}!=null?"Surveyors Comments: "+$F{note}+". ":""))

 

It works great if all fields are filled but if i dont fill old_tag1 for example i get the text "Tag Number:" +", ".

 

Any help would be gratefully recieved!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...