I have tried to insert date and time string formatted into mysql timestamp field by using following two methods but both shows me 0000-00-00 00:00:00
INSERT INTO test VALUES ( UNIX_TIMESTAMP('2013-08-05 18:19:03') )
INSERT INTO test VALUES ( UNIX_TIMESTAMP(STR_TO_DATE('2013-08-05 18:19:03', '%Y-%m-%d %H:%i:%s')) )
I believe first one should work as I am expecting but not sure why isn't parsing date and time?
The problem is that your field is defined as TIMESTAMP but UNIX_TIMESTAMP returns an int. Use INSERT INTO test VALUES ('2013-08-05 18:19:03' ) instead.
Related
I have a table with date values stored as strings, like '2012-01-15'. Some of them are invalid, like '2012-04-31'. I would like to insert the valid dates into a DATE type column in another table, and default the day to 1 if it is too large for the month.
DAYNAME seems to be the only function in MySQL that will check whether a date is valid. However, it issues a warning for an invalid date (in addition to returning NULL), which upgrades to an error in an INSERT or UPDATE statement.
So I'd like to do something like
INSERT INTO date_tbl (date_value)
SELECT IF(DAYNAME(date_string) IS NOT NULL, date_string, CONCAT(LEFT(date_string, 8), '1')
FROM date_string_table;
This fails with Data truncation: Incorrect datetime value: '2010-04-31' even though I am not actually inserting invalid data.
The problem with using INSERT IGNORE is running the risk of actually inserting invalid data, which I would really like to avoid.
EDIT Oct 5:
This problem can be reproduced without creating the intermediate table simply as
CREATE TABLE date_tbl (
date_val DATETIME
);
INSERT INTO date_tbl (date_val)
SELECT IF(DAYNAME('2012-04-31') IS NOT NULL, '2012-04-31', NULL);
I would like the above INSERT to insert NULL for that invalid date, instead of failing.
You can compare days of the proper date and last day for that month with LAST_DAY and STR_TO_DATE.
So your query would be:
INSERT INTO date_tbl (date_val)
SELECT IF(DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d')) > DAY(LAST_DAY(STR_TO_DATE('2012-02-30','%Y-%m-%d'))), NULL,'2012-02-30');
DB Fiddle
A workaround is to use INSERT IGNORE and then validate after the fact:
SELECT date_value
FROM date_tbl
WHERE DAYNAME(date_value) IS NULL;
Should return zero rows.
CREATE TABLE orders(
order_id INTEGER PRIMARY KEY,
customer_id INTEGER,
order_date DATE,
ship_date DATE
);
INSERT INTO orders
VALUES
(1, 1200, '2013-23-10', '2013-28-10');
Is there anything wrong with the above code?
You are using 'YYYY-dd-MM' format whereas you need to use 'YYYY-MM-dd', try the following:
INSERT INTO orders VALUES (1, 1200, '2013-10-23', '2013-10-28');
Date format is 'YYYY-MM-DD'.
23 and 28 are not valid values for MM month component.
The error returned by MySQL is expected behavior.
Nothing necessarily wrong with what you are doing, if you are want MySQL to return an error message.
If you are wanting the statement execution to be successful, and to add a row to orders table, supply valid date values for the DATE columns.
Either change the literal values to match the format expected by MySQL, or use STR_TO_DATE function to convert the string from a specified format.
... , '2013-10-23' , ...
or
... , STR_TO_DATE('2013-23-10','%Y-%d-%m') , ...
I have an INSERT query in ruby and I'm passing parameters from another table. One of the parameters is a timestamp value, for example: 2015-11-22 12:57:06 +0000 which is stored in a variable name created_at (of type Time)
insert into my_tbl set
name = '#{name}',
created_at = #{created_at}
and I'm always getting errors while trying to insert it.
I've tried to convert it to string, and to use str_to_date function, but the problem is that I have a timestamp value.
How can I insert the value to the table?
INSERT INTO my_tbl (name, created_at)
VALUES (name, created_at.to_s.split(' +').first)
Format the input in mysql from chat and from ruby program insert and format in this chat with the sale format
Update:
In mysql :
Select to_date('"+varchar+"','dd/mm/yyyy'......
In Ruby:
Varchar='01/12/2015"
Of course with format
So here's the thing, I have the database 'example' with two tables:
'exampletable1' contains two columns 'ID','datetime'
'exampletable2' contains two columns 'id2','timestamp'
(both ID fields are primary keys and auto incremented)
I'm trying to import elements (rows) from exampletable1 to exampletable2, but when it's giving me NULL in the 'timestamp' field. so I tried to use the FORMAT(), UNIX_TIMESTAMP you talked about, but it's not working, neither the code you posted.
Here's the queries I'm executing:
INSERT INTO `exampletable2`(`timestamp`)
SELECT UNIX_TIMESTAMP(`datetime`) FROM `exampletable1`
or:
INSERT INTO `exampletable2`(`timestamp`)
SELECT FORMAT(`datetime`,TIMESTAMP) FROM `exampletable1`
I appreciate any help guys!
You don't need to reformat/convert anything. Just do
INSERT INTO exampletable2 (timestamp)
SELECT datetime
FROM exampletable1
Here is SQLFiddle demo
Recommended reading
The DATE, DATETIME, and TIMESTAMP Types
You don't need to convert DATETIME to TIMESTAMP, just insert them directly:
INSERT INTO `exampletable2`(`timestamp`)
SELECT `datetime` FROM `exampletable1`
SQLFiddle
You can check more about this type here: The DATE, DATETIME, and TIMESTAMP Types
I have one column date1 which is varchar type
I want this column to date type.
I tried changing field but all date is converted to 0000-00-00.
format is dd-mm-yyyy but in varchar.
How can I convert the same date format but with date format using sql queries or similar but at database level ?
UPDATE `table`
SET `column` = str_to_date( `column`, '%d-%m-%Y' );
More about STR_TO_DATE function.
Since your column name is date1, you can replace column with date1 in the above syntax, and the code shall be:
UPDATE `table`
SET `date1` = str_to_date( `date1`, '%d-%m-%Y' );
The other answers here are risky, because if they go wrong you'll lose your data. A safer way to do this is to create a new field on your database with a DATE (or DATETIME if you need time as well) format, then to run a query like
UPDATE `table` SET `my_new_date_field` = STR_TO_DATE( `my_old_data_field`, '%d/%m/%Y');
In this way, if the %d/%m/%Y bit is wrong, you won't lose your data.
Once you're happy, you can delete the old data field and rename the new one.
use STR_TO_DATE Function of MySQL
FIRST you will need to update the value in date format.
UPDATE `tbl` SET `date1` = STR_TO_DATE(`date1`, '%d-%m-%Y') WHERE 1=1
THEN Convert the field to date.
Most importantly remember to insert date as Y-m-d format, after then.