Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[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
Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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.

View solution in original post

2 Replies
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

This is sweet! Thanks rhall