Concatenating 2 fields, one of them null

0

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} </td></tr></tbody></table>
pennywiser's picture
Joined: May 13 2009 - 4:20am
Last seen: 10 years 8 months ago

12 Answers:

0

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. 
 
hangman01's picture
150
Joined: Jun 21 2007 - 10:00am
Last seen: 12 years 7 months ago
0

You don't need to put this in a variable. You can put it right into the field on your report.

svenn's picture
1393
Joined: Mar 19 2007 - 5:57am
Last seen: 12 years 10 months ago
0

That is a good idea svenn.

hangman01's picture
150
Joined: Jun 21 2007 - 10:00am
Last seen: 12 years 7 months ago
0

How would i extend this to consider 8 fields any number of which could be filled?

 

Many Thanks

 

Will

gardnerw's picture
Joined: Oct 13 2009 - 6:28am
Last seen: 10 years 3 months ago
0

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

slow's picture
911
Joined: Feb 17 2010 - 6:57am
Last seen: 9 years 11 months ago
0
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,
gardnerw's picture
Joined: Oct 13 2009 - 6:28am
Last seen: 10 years 3 months ago
0
Sorry i get it, i'm being dopey!

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

Thanks for your help again slow!
gardnerw's picture
Joined: Oct 13 2009 - 6:28am
Last seen: 10 years 3 months ago
0

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
slow's picture
911
Joined: Feb 17 2010 - 6:57am
Last seen: 9 years 11 months ago
0
:) Defo worth the extra karma points for taking the time and patience.

Thanks Slow!
gardnerw's picture
Joined: Oct 13 2009 - 6:28am
Last seen: 10 years 3 months ago
0

double post... I'm having problems with jasperserver forum :)

 



Post Edited by slow at 05/27/2010 10:04
slow's picture
911
Joined: Feb 17 2010 - 6:57am
Last seen: 9 years 11 months ago
0

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
slow's picture
911
Joined: Feb 17 2010 - 6:57am
Last seen: 9 years 11 months ago
0

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!

gardnerw's picture
Joined: Oct 13 2009 - 6:28am
Last seen: 10 years 3 months ago
Feedback