Parsing Custom Date Format in MySQL - mysql

Problem
I need to query out a date value for use in some ETL processing. However, the data is stored in MySQL as a VARCHAR column in values like 1/1/19.
What I've tried
A simple CAST(myDateColumn as DATE) fails as I get values like 0001-01-19 returned back.
Question
Am I able to pass a custom date format string into the CAST call somehow to tell it how to parse out the date parts? If not, could a SUBSTRING type of function help here?

As discussed in the comments above, you can use the STR_TO_DATE() function to parse a string into a proper YYYY-MM-DD date.

Related

Keep getting informatica function error invalid string for converting to Date

I am trying to convert a nvarchar into a date, but I keep getting this error below
TT_11132 Transformation [Expression] had an error evaluating output column [Run_Date1]. Error message is [<> [TO_DATE]: invalid string for converting to Date ... t:TO_DATE(u:'20190304',u:'MM/DD/YYYY HH24:MI:SS')].
but my function doesn't include a 'MM/DD/YYYY HH24:MI:SS'. Here is my function for reference.
TO_DATE(TO_CHAR(to_date(RUN_DATE),'mmddyyyy'),'mm/dd/yyyy')
Please Help
Thank you
Samik is right, just use the TO_DATE(RUN_DATE, 'YYYYMMDD')
What I'd like to add is some explanation why your function is wrong. So stripping it from innermost:
to_date(RUN_DATE)
Converts the RUN_DATE string to a DATE trying to guess the format as it has not been given. Let's call the result of it as NewDate and analyze further:
TO_CHAR(NewDate,'mmddyyyy')
Now this will convert NewDate to a string in the mmddyyyy format. Let's call this one as NewString_MMDDYYYY and see where it gets us. This leaves the final function as:
TO_DATE(NewString_MMDDYYYY,'mm/dd/yyyy')
See the error yet? You tell Informatica to convert NewString_MMDDYYYY to a date. And you tell the function that it is written in the mm/dd/yyyy format - which obviously is not being used, as the TO_CHAR function was told to store it in mmddyyyy.
So, if your input RUN_DATE is in YYYYMMDD format, then all you need to do is TO_DATE(RUN_DATE, 'YYYYMMDD'). If another formati is used by RUN_DATE, just replace the second argument accordingly.
Replace everything with just TO_DATE(RUN_DATE, 'YYYYMMDD')

Talend MySql String To MySql Date for storage and difference calculations

I have a MySql table with a String 2014-02-21 16:53:01 stored in a varchar column. I need to use Talend Data Integration to convert it into a date for calculations in the format dd/mm/yyyy hh:mm:ss and to store it in that format in another MySql as a date column after the calculations.
I have a talend tmap component but I get parsing errors and not sure how to solve it and the general sequence of steps needed.
In the tmap I have a variable with the expression
row1.date !=null ? TalendDate.parseDate("dd/mm/yyyy hh:mm:ss",row1.date):null
Which I hope will load the variable wiIs that the best way to
Convert the string in the source table to a date in the format I need?
How do you handle null dates as I need to get the difference between 2 dates but handle the situation where 1 or both may be blank or null
How do I get the destination table to store the date in the format I need? I have the Date Pattern supplied in the destination tmap schema but is that enough if the date format is yyyy-mm-dd ?
Any Talend experts able to help a novice out?
For better control on null check I recommend you to use below function.
!Relational.ISNULL(row1.date) && !"".equalsIgnoreCase(row1.date) ?TalendDate.parseDate("MM/dd/yyyy HH:mm",row1.date):(null or default date);
if you are doing any operation on date? then don`t supply null as a result because you may get error on next component.
always pass default value so that you can check in next component and ignore it or store it based on business need.
You can try below condition which will take care of blank string also;-
row1.date !=null && !"".equalsIgnoreCase(row1.date)? TalendDate.parseDate("MM/dd/yyyy HH:mm",row1.date) : null
Thanks for the help. I decided to configure the schema for the database input to identify the dates as the date datatype instead of string which got Talend to do the work of converting
The pointers on validating the date were useful as well. Thanks for the help UmeshR

MySQL date format is not supported

When we call this procedure
Call get_reports_by_time('2014-12-22 11:19:26 AM').
no data is returned
but when we call this
Call get_reports_by_time('2014-12-22 11:19:26 ')
all the records for that day is returned. why its so?
string representation of dates/timestamps,... is dependent on your db settings.
In your case one representation can be converted to a date (assuming that you work with a date type in your stored procedure) and one representation cannot be converted to a date.
I higly suggest to use date and time types and not any string representation for your paremeters (if you need a string representation then convert it by YOURSELF using date/str_to_date)

Convert varchar string data to time format in mysql

I need to convert varchar string data to time format in mysql.
For example,
I have a varchar column in table which stores time. The accepted values should be like 9:30 AM or 1200 PM. But currently it has either blank values or it has values like 9.30am or 12:00
There are many records like this, so cannot update manually.
Ithere any work around or function or procedure to do so?
please help.
Thanks
You can use the STR_TO_DATE() MySQL function to convert any string to a date.
Additionally you can use TIME() to extract the time portion of a datetime. A combination of both function is used to convert an arbitrary date string to a datetime and then you can extract the time portion from it as a valid MySQL TIME.
By default MySQL functions follow standard format but custom format can be specified and if your values don't use the international formats you'll need to check with the documentation and provide the format your system is using.

MySQL - How to parse a string value to DATETIME format inside an INSERT statement?

I have a MySQL database, with a column that is date type DATETIME.
I am getting a string value for a date / time from an external application. That string value looks like this:
'5/15/2012 8:06:26 AM'
MySQL throws an error on the INSERT: "Error. Incorrect datetime value". My workaround was to change the column type to VARCHAR, which works, but I really need the data as a proper Date & Time for future use.
I researched accepted formatting for MySQL DATETIME values, and found that MySQL wants the DATETIME format as 'YYYY-MM-DD HH:MM:SS'.
I can't change the external application to reformat the date / time string in a format, so my only chance is to deal with it.
What I need to do, I think, is parse the existing string, using MySQL syntax, inside of my INSERT statement, but I'm not sure how to do that. I have some idea that I need to use a SELECT clause, perhaps STR_TO_DATE, somehow in combination with my INSERT statement.
Here is my current INSERT statement. I removed the other fields that are not causing a problem, just to make the example clean.
INSERT INTO tblInquiry (fldInquiryReceivedDateTime) VALUES ('5/15/2012 8:06:26')
Use MySQL's STR_TO_DATE() function to parse the string that you're attempting to insert:
INSERT INTO tblInquiry (fldInquiryReceivedDateTime) VALUES
(STR_TO_DATE('5/15/2012 8:06:26 AM', '%c/%e/%Y %r'))