on 03-26-2010 3:32 PM
Hi
I need a UDF Code for the following requirement ,which is used in various fields across a lot of mappings
Requirement is that if it is a fixed length file and the field length is 10 then whatever value thats coming should be right alligned and there shud be zeroes padded to the left of it
for e.g
field length is 10
input value os 3456
then the output should be 0000003456
I want to make it a generic function wher i can take the field length and the input value as inputs
Dev
Hi,
If your input is always an numeric one then you can use formatnum as mentioned by sarvesh. Here if you get alphanumeric then this will throw exception. Then you need an udf like this:
Create a udf with one argument as input and add this code (here I am assuming your length of the output is 10 or change accordingly)::
int len=input.length();
for(int i=0; i<10-len;i++)
{
input="0"+input;
}
return input;
Here this will work for both numbers and alphabets.
Regards,
---Satish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Use the below code
Get two inputs as parameter ( one for length, second the value)
example: (10, 1234) or (14,900023)
int len = Integer.parseInt(a);
StringBuffer sb = new StringBuffer();
for(int i=b.length();i<len;i++)
{
sb.append("0");
}
sb.append(b);
return sb.toString();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No need of UDF, just use the std. function called FormatNum.
Double click the function and enter 10 zeros in first input box and do your mapping.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sarvesh
Why 10 zeroes ?
my requirement is
for a field length of 5 if the input is 42 the output should be 00042
similarly for a field length of 8 if the input is 876 then the value should be
00000876, if input is 9876 then it shud be 00009876
and so on ...
will FormatNum take care of right allignment also ?
Dev
Edited by: sd on Mar 26, 2010 4:44 PM
>
> Hi Sarvesh
>
> Why 10 zeroes ?
> my requirement is
>
> for a field length of 5 if the input is 42 the output should be 00042
>
> similarly for a field length of 8 if the input is 876 then the value should be
> 00000876, if input is 9876 then it shud be 00009876
>
> and so on ...
> will FormatNum take care of right allignment also ?
>
> Dev
>
> Edited by: sd on Mar 26, 2010 4:44 PM
Just try the function and your all queries will be answered.
Hi,
You can edit my function. What you can do is send the length as one input and then it should work. Add this code:
Create a value udf with two input arguments a and input. Then add this code:
Imports: java.*;
int len=input.length();
int b = Integer.parseInt(a);
for(int i=0; i<b-len;i++)
{
input="0"+input;
}
return input;
Here pass the total length of the field in first argument and then send the input value to teh second argument. Then it should work for you.
Regards,
---Satish
User | Count |
---|---|
84 | |
10 | |
10 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.