Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

String to date conversion

My input from database is having "date" in string datatype with size 50, which is sending date in yyyy-mm-dd format. I want to store it at the destination in String(50) with mm-dd-yyyy format. Solution please.

Labels (2)
1 Solution

Accepted Solutions
TRF
Creator III
Creator III

"".equals(row2.DiagnosisActivityStartDate) || 
row2.DiagnosisActivityStartDate == null ? "" : TalendDate.formatDate("MM-dd-yyyy",TalendDate.parseDate("yyyy-MM-dd",row2.DiagnosisActivityStartDate))

View solution in original post

13 Replies
KarthikGs
Creator
Creator

hi,

to convert string to date you have to use talendDate routines available with in the talend code repo.

Here is how you can convert a string in to date.

TalendDate.parseDate("format date of the string to be parsed", "string in the format of the date to be parsed")

And to limit the length of the string, define its size in the schema.

 

Thanks

TRF
Creator III
Creator III

Try this:
TalendDate.formatDate("mm-dd-yyyy", TalendDate.parseDate("yyyy-mm-dd", row1.yourDatabaseField))
Anonymous
Not applicable
Author

I am getting error. Actually, the input field which is sending date is string(50) and output field is also in string(50). Please suggest.

 

Or, Can we just shuffle it using only string function? like, currently i am getting yyyy-mm-dd, and shuffle it to mm-dd-yyyy. is it possible with any function? 

Anonymous
Not applicable
Author

Hello,

What's the error are you getting?

Could you please post your current job setting screenshot on forum? Are you using tMap component in your job?

Best regards

Sabrina

Anonymous
Not applicable
Author

i have attached the screen shot, please suggest

 

error is:

 

[statistics] connecting to socket on port 3456
[statistics] connected
java.lang.NumberFormatException: For input string: "row2"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at routines.system.FastDateParser$DateParser.parse(FastDateParser.java:127)
at java.text.DateFormat.parse(Unknown Source)
at routines.TalendDate.parseDate(TalendDate.java:884)
at routines.TalendDate.parseDate(TalendDate.java:842)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_5Process(OCM_Data_Extraction.java:2846)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_2Process(OCM_Data_Extraction.java:1014)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.runJobInTOS(OCM_Data_Extraction.java:3648)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.main(OCM_Data_Extraction.java:3497)
Exception in component tMap_2 (OCM_Data_Extraction)
java.lang.RuntimeException: java.text.ParseException: Unparseable date: "row2.DiagnosisActivityStartDate"
at routines.TalendDate.parseDate(TalendDate.java:898)
at routines.TalendDate.parseDate(TalendDate.java:842)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_5Process(OCM_Data_Extraction.java:2846)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_2Process(OCM_Data_Extraction.java:1014)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.runJobInTOS(OCM_Data_Extraction.java:3648)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.main(OCM_Data_Extraction.java:3497)
Caused by: java.text.ParseException: Unparseable date: "row2.DiagnosisActivityStartDate"
at java.text.DateFormat.parse(Unknown Source)
at routines.TalendDate.parseDate(TalendDate.java:884)
... 5 more
[statistics] disconnected


Graph1.jpg
TRF
Creator III
Creator III

You need to remove double quotes arround row2.DiagnosisActivityStartDate in tne Modified_Data flow
Anonymous
Not applicable
Author

Thanks TRF.... its resolved.... 

Anonymous
Not applicable
Author

earlier it ran fine. Now its showing below error.

 

I just noticed that, some of the fields from the input is having blank in date field. How to handle this

 

statistics] connecting to socket on port 4016
[statistics] connected
java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.substring(Unknown Source)
at routines.system.FastDateParser$DateParser.parse(FastDateParser.java:127)
at java.text.DateFormat.parse(Unknown Source)
at routines.TalendDate.parseDate(TalendDate.java:884)
at routines.TalendDate.parseDate(TalendDate.java:842)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_5Process(OCM_Data_Extraction.java:2762)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_2Process(OCM_Data_Extraction.java:998)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.runJobInTOS(OCM_Data_Extraction.java:3589)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.main(OCM_Data_Extraction.java:3438)
Exception in component tMap_2 (OCM_Data_Extraction)
java.lang.RuntimeException: java.text.ParseException: Unparseable date: ""
at routines.TalendDate.parseDate(TalendDate.java:898)
at routines.TalendDate.parseDate(TalendDate.java:842)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_5Process(OCM_Data_Extraction.java:2762)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.tDBInput_2Process(OCM_Data_Extraction.java:998)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.runJobInTOS(OCM_Data_Extraction.java:3589)
at ocm.ocm_data_extraction_0_1.OCM_Data_Extraction.main(OCM_Data_Extraction.java:3438)
Caused by: java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(Unknown Source)
at routines.TalendDate.parseDate(TalendDate.java:884)
... 5 more
[statistics] disconnected

TRF
Creator III
Creator III

Should be a new case...

Anyway, you may exclude records when the data content lenght is less than 10 characters.

Use a tFilterRow for that.