cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for Date format

Former Member
0 Kudos

Hi,

I want to write an UDF

I want to pass Date as an String in format yyyymmdd.

I want to convert it to mm/dd/yyyy. & pass new date as o/p/

But there is a possibility that i/p Date may come as blank in that case I want to keep output as blank.

Please help

Accepted Solutions (1)

Accepted Solutions (1)

santhosh_kumarv
Active Contributor
0 Kudos

Hi,

No need of UDF to transform the date format. You can use the standard date function Date Trans.

>>But there is a possibility that i/p Date may come as blank in that case I want to keep output as blank.

Chech if date is Blank using IF condition in yes pass a blak value else pass the date to the standard function and pass the transformed date.

Former Member
0 Kudos

Hi,

Thanks.

I am not sure How can I check the same date for if it is blank or not and pass it to transform func at the same . Please can you send the graphical mapping ?

Thanks again

Former Member
0 Kudos

Hi,

you could insert the source field two times in mapping.

It looks like:

Source -> length -> if (>1)

then Source TransformDate (yyyyMMdd, MM/dd/yyyy)

else Constant("")

Regards

Patrick

Former Member
0 Kudos

Sorry I should have said this :

Date is coming from another UDF. so can't take it from there twice , can I ?

santhosh_kumarv
Active Contributor
0 Kudos

Ohh.. Patrick has a way with using the length function.

You could also do this

<sourcedate> & constant ("") ----------------> equalsS(String Fn) ------------> IF.
constant ("") ------------> Then (part of IF)
<sourcedate> ------------> Trans Date -----------> else (part of IF).

Thanks

SaNv...

Former Member
0 Kudos

Hi,

you could maybe use the other UDF also two times.

Otherwise you could use a new UDF and inside you check if the input is blank and if not you could use the TransformDate function inside of the UDF:

Regards

Patrick

Edited by: Patrick Koehnen on Oct 8, 2008 11:53 AM

former_member203631
Participant
0 Kudos

pass the input date field to MAPWITHDEFAULT function then map it to target field

--

Shiva.

santhosh_kumarv
Active Contributor
0 Kudos

>>so can't take it from there twice , can I ?

You Can... repeat the same UDF mapping for the other input also.

Thanks

SaNv...

Former Member
0 Kudos

Thanks guys,

I am using this code but get an error

int len;

len = Date.length();

if (len>7)

{

String year = Date.substring (0,4);

String month = Date.substring (4,2);

String day = Date.substring (6,2);

String f = month + "/" + day + "/" + year;

return f;

}

else

{

return Date;

}

--- Here when I run this in test mode I get this exception -

Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -2] in class com.sap.xi.tf._MM_PSN_EMP_INFO_ method check$[19880517, B1, A1, C1, F1, G1, H1, X1, X1, X1, X1, X1, X1, 07, 5, com.sap.aii.mappingtool.tf3.rt.Context@7ab97403]

Please help.

former_member203631
Participant
0 Kudos

Vibhav,

Did you tried with MAPWITHDEFAULT function.

---

Shiva.

Former Member
0 Kudos

Hi,

you could try to use the standard function inside of the UDF like I mentioned above.

Regards

Patrick

santhosh_kumarv
Active Contributor
0 Kudos

Hi,

>>String month = Date.substring (4,2);

>>String day = Date.substring (6,2);

Index value is wrong. It should be (4,6) and (6,8).

Thanks

SaNv...

Former Member
0 Kudos

Damn..

Thanks Santhosh.

Edited by: Vaibhav Bhandari on Oct 8, 2008 11:02 PM

Former Member
0 Kudos

make some changes in ur udf

int len;

len = Date.length();

if (len == 10)

{

String year = Date.substring (0,4);

String month = Date.substring (5,7);

String day = Date.substring (8,10);

String f = month + "/" + day + "/" + year;

return f;

}

else

{

return Date;

}

Answers (2)

Answers (2)

ParvathyS_SAP
Product and Topic Expert
Product and Topic Expert
0 Kudos

check ur substring indexing

Former Member
0 Kudos

use the below udf


public String date_conversion(String a,Container container)
{
 //write your code here
 if(a.length() > 0)
  {
     String year = a.substring(0,4);
     String month = a.substring(5,7);
     String day = a.substring(8,10);
     String output = month + "/" +  day + "/" + year;
     return output;
  }
else
 return " ";
}

jst pass the output of previous udf to the above udf