Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[resolved] How to pass only the first 40 characters of a string using tMap?

Hi, my input has strings as long as 80 characters in a certain field
In the output field I only want up to the first 40 characters
How can I set this using the tMap Expression Builder?
Thank You 0683p000009MACn.png
Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

That method will make you run into a myriad of problems, as it will crash when that column is empty in a row, or when the string is shorter than 40 characters. Generally not what you want or need in a data integration solution.
You'll want to use String.format(format, inputString)
The format is a bit complex to use, so if you want to get more out of it, you'll have to google it yourself. However, for your problem, you'll need this:
"%.40s"
So the full method is:
String.format("%.40s", inputString)
In general, the format works like this for strings:
%<number1>.<number2>s
The % denotes an argument of the function. The first time it's the second argument (i.e. the one after the format), the next time it's the third, and so on. So you can write String.format("%.6s%.2s", LastName, FirstName) and you'll get a string consisting of the first six letters of the last name and the first two letters of the first name.
number1 is the minimum amount of characters in the string. The method will add spaces at the end.
The point is just a separator. It comes from the formatting of numbers, I think, where the number before the point is number of digits before the decimal point, and the number after is the number of digits after the decimal point.
number2 is the maximum amount of characters in the string. If there's more, they'll be cut off.
The s stands for string, other inputs require different identifiers here (e.g. f is for float).

View solution in original post

8 Replies
Anonymous
Not applicable
Author

Hi,
Try to use method substring(int beginIndex, int endIndex) in tMap expression.
See my screenshots
Best regards
Sabrina
0683p000009MBTv.png 0683p000009MBK1.png 0683p000009MBh9.png
Anonymous
Not applicable
Author

That method will make you run into a myriad of problems, as it will crash when that column is empty in a row, or when the string is shorter than 40 characters. Generally not what you want or need in a data integration solution.
You'll want to use String.format(format, inputString)
The format is a bit complex to use, so if you want to get more out of it, you'll have to google it yourself. However, for your problem, you'll need this:
"%.40s"
So the full method is:
String.format("%.40s", inputString)
In general, the format works like this for strings:
%<number1>.<number2>s
The % denotes an argument of the function. The first time it's the second argument (i.e. the one after the format), the next time it's the third, and so on. So you can write String.format("%.6s%.2s", LastName, FirstName) and you'll get a string consisting of the first six letters of the last name and the first two letters of the first name.
number1 is the minimum amount of characters in the string. The method will add spaces at the end.
The point is just a separator. It comes from the formatting of numbers, I think, where the number before the point is number of digits before the decimal point, and the number after is the number of digits after the decimal point.
number2 is the maximum amount of characters in the string. If there's more, they'll be cut off.
The s stands for string, other inputs require different identifiers here (e.g. f is for float).
Anonymous
Not applicable
Author

Hi to both, thanks for the suggestions and explanation
I think for now I will address this problem in my SQL query instead then
Anonymous
Not applicable
Author

ddctf,
If you are going to use Studio much, follow Elmer's advice when you have time to read up on it. Leaning on SQL for this kind of stuff will slow you down.
Talend people - someone (who you pay 0683p000009MACn.png) should write "Java for Talend." Even for people who know java, this kind of insight is important to get your product wider acceptance. Java's relation to Studio isn't just a great strength, it's your biggest obstacle, too. There's no reason you should let every new prospect figure out how to do this kind of thing themselves or cast Talend aside for some product with a stripped down pascal interpreter.
Anonymous
Not applicable
Author

But honestly, Levin,
what is hard to learn on doing a "LEFT(<field>,<length>)" in SQL? 0683p000009MA9p.png (even with the advance, that it's much faster)
But i also strongly recommend Elmers solution if you're working on large, long grown datastocks, you never now what you will get on the next run 0683p000009MA9p.png
Greets,
M
Anonymous
Not applicable
Author

But...
If one wants to use tMap and not SQL - why not StringHandling.LEFT(<field>,<length>) ?
It works with null value, empty string, strings shorter than expected length...
Anonymous
Not applicable
Author

Hi All 
i have string like 'B0000790'
and result has to be like this 'B000    0790'
there are 4 spaces has to be added
please tell how can we do this
Help much appreciated
Thanks.
Anonymous
Not applicable
Author

You can achieve your required result using Talend functions something like this :-
StringHandling.LEFT(<YOUR_FIELD>, 4) + StringHandling.SPACE(4) + StringHandling.RIGHT(<YOUR_FIELD>, 4)

It might need a little refinement, but works OK in principle.