[resolved] How to truncate in tMap variable expressions

Highlighted
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

Accepted Solutions
Community Manager

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.

All Replies
Community Manager

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.
One Star

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

This is sweet! Thanks rhall

15TH OCTOBER, COUNTY HALL, LONDON

Join us at the Community Lounge.

Register Now

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.

Download