One Star

[resolved] How to truncate in tMap variable expressions

Hi there,
I want to truncate a string so that it is small enough to fit into an external DB from a Talend csv file output. What I have is the following:
row1.OOH_Type.substring(0,150)

However, I get an index out of range error in the tmap component because I suspect that there are blank cell values which is throwing up null values that causes exceptions when I try to truncate a value.
Anyone know an expression I can write to take care of null values as well as truncate my string to the required size at the same time?
Thanks,
Facoda
1 ACCEPTED SOLUTION

Accepted Solutions
Fifteen Stars

Re: [resolved] How to truncate in tMap variable expressions

This is not to do with nulls. You cannot have null as part of a String. Null is literally nothing. This is caused by the value you are trying to truncate not being 150 characters long. To get round this you can use something like below...
row1.OOH_Type!=null && row1.OOH_Type.length()>150 ? row1.OOH_Type.substring(0,150) : row1.OOH_Type  

First we check to make sure the value is not null, then we check to see that the length is greater than 150 chars. If both are true, then we substring to get a 150 char String, otherwise we just return the original value.
Rilhia Solutions
2 REPLIES
Fifteen Stars

Re: [resolved] How to truncate in tMap variable expressions

This is not to do with nulls. You cannot have null as part of a String. Null is literally nothing. This is caused by the value you are trying to truncate not being 150 characters long. To get round this you can use something like below...
row1.OOH_Type!=null && row1.OOH_Type.length()>150 ? row1.OOH_Type.substring(0,150) : row1.OOH_Type  

First we check to make sure the value is not null, then we check to see that the length is greater than 150 chars. If both are true, then we substring to get a 150 char String, otherwise we just return the original value.
Rilhia Solutions
One Star

Re: [resolved] How to truncate in tMap variable expressions

This is sweet! Thanks rhall