1

I was loading DB2 table into mysql and I was facing issue while loading data with Date format.

I converted those columns into VARCHAR(26) format and loaded the data successfully.

Now I want to convert 2003-01-13-16.50.32.000000 into Date format.

I introduced another column with Date datatype and tried to update it using origional (String) column.

update po_datamart
    set POH_APPROVED_DATE_TEMP=DATE_FORMAT(str_to_date(POH_APPROVED_DATE,'%Y-%c-%d-%H.%i.%s'), '%Y-%c-%d%H:%m:%s');

I am getting following error "Error Code: 1292. Truncated incorrect datetime value: '2003-01-13-16.50.32.000000'".

Would appreciate if anyone can help me with this VARCHAR to DATE conversion.

1
  • Please avoid blasting every answer with the same verbose comment. Instead, you could update your question. Commented Jul 13, 2016 at 9:06

4 Answers 4

1

Use STR_TO_DATE with an appropriate format mask:

SELECT STR_TO_DATE('2003-01-13-16.50.32.000000', '%Y-%m-%d-%H.%i.%s.%f')

If you wanted to update:

UPDATE po_datamart
SET POH_APPROVED_DATE_TEMP = STR_TO_DATE(POH_APPROVED_DATE, '%Y-%m-%d-%H.%i.%s.%f')

Follow the link below for a running demo:

SQLFiddle

Sign up to request clarification or add additional context in comments.

6 Comments

Hello Tim. Thanks for your reply. When I fire your select query, I get response as NULL value. When I run your update query, I get "Error Code: 1411. Incorrect datetime value: '2003-01-13-16.50.32.000000' for function str_to_date"
@user2663104 Sorry I have updated and it works now. I was using %h for 12 hour time instead of %H for 24 hour time.
UPDATE po_datamart SET POH_APPROVED_DATE_TEMP = STR_TO_DATE(POH_APPROVED_DATE, '%Y-%m-%d-%H.%i.%s.%f') - This query worked but again ended with response - "Error Code: 1411. Incorrect datetime value: '+0.00000000000000E+000' for function str_to_date"
What actual string data are you feeding in? It looks like you are using a non standard format.
I used .del file, It is a dump file of the old Data warehouse table which was there in DB2. I used LOAD INLINE functionality to upload the data in the table(MySQL).
|
0
UPDATE      po_datamart
    SET     POH_APPROVED_DATE_TEMP = str_to_date(POH_APPROVED_DATE, '%Y-%m-%d-%H.%i.%s.%f');

2 Comments

UPDATE po_datamart SET POH_APPROVED_DATE_TEMP = STR_TO_DATE(POH_APPROVED_DATE, '%Y-%m-%d-%H.%i.%s.%f') - This query worked but again ended with response - "Error Code: 1411. Incorrect datetime value: '+0.00000000000000E+000' for function str_to_date"
What version of MySQL are you using?
0

I think you only need and space, try with:

set POH_APPROVED_DATE_TEMP=DATE_FORMAT(str_to_date(POH_APPROVED_DATE,'%Y-%c-%d-%H.%i.%s'), '%Y-%c-%d %H:%m:%s');

I change the last string '%Y-%c-%d%H:%m:%s' to '%Y-%c-%d %H:%m:%s'.

Update for a date column:

set POH_APPROVED_DATE_TEMP=DATE_FORMAT(str_to_date(POH_APPROVED_DATE,'%Y-%c-%d-%H.%i.%s'), '%Y-%c-%d');

7 Comments

Thanks for your reply. I got the response as "Error Code: 1292. Truncated incorrect date time value: '2003-01-13-16.50.32.000000'"
I try the code and seems to work correctly; what data type have POH_APPROVED_DATE_TEMP (date, datetime, other?). If the column is date type then the data is truncated, you use 2003-01-13 16:50:32 with a column that only admits 2003-01-13. I edit the answer accordingly.
I am using Date. Should I use datetime?
Read my answer, the first code if for datetime column, but I made an update for date column. Use as you wish.
UPDATE po_datamart SET POH_APPROVED_DATE_TEMP = STR_TO_DATE(POH_APPROVED_DATE, '%Y-%m-%d-%H.%i.%s.%f') - This query worked but again ended with response - "Error Code: 1411. Incorrect datetime value: '+0.00000000000000E+000' for function str_to_date"
|
0

Try this:

update po_datamart set POH_APPROVED_DATE_TEMP = str_to_date(POH_APPROVED_DATE, '%Y-%m-%d-%H.%i.%s.%f');

5 Comments

Thanks for your reply. Error Code: 1292. Truncated incorrect date time value: '2003-01-13-16.50.32.000000' Got above error.
Is POH_APPROVED_DATE's type is varchar or date?
POH_APPROVED_DATE's type is varchar
@user2663104 Updated my answer, please check it again.
UPDATE po_datamart SET POH_APPROVED_DATE_TEMP = STR_TO_DATE(POH_APPROVED_DATE, '%Y-%m-%d-%H.%i.%s.%f') - This query worked but again ended with response - "Error Code: 1411. Incorrect datetime value: '+0.00000000000000E+000' for function str_to_date"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.