Converting datetime to unix timestamp in mySQL - mysql

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

Related

Insert a date in MySQL only if it is valid

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.

MySQL time type

CREATE TABLE blurt (blurtid integer, btime datetime );
I did this query above and I got an error.
The reason is probably because "btime" from the data is something like 1/22/12. However, datetime is something like 1-22-2012.
Which type should btime be?
Thanks guys!
The following create table statement should not cause an error:
CREATE TABLE blurt (blurtid integer, btime datetime );
But trying to insert '1/22/12' into a datetime column will cause error. MySQL has a function called STR_TO_DATE which can help with that:
INSERT INTO blurt(1, STR_TO_DATE('1/22/12', '%m/%d/%y'))
If your dates had the format '2012-01-22' then you could insert them directly. By the way, you are making the right decision by using a date type to store your date information.

Mysql LONGTEXT With timestamp string to actual TIMESTAMP

I'm stumped on this one and need some help for the first time.
I've got a database field which is mysql LONGTEXT, with a string for a timestamp, eg, the string "1448386279"
I need to get that into a proper MySql TIMESTAMP field.
So
insert into temp (timestampfield) VALUES (UNIX_TIMESTAMP('1448377601'));
insert into temp (timestampfield) values(cast('1448377601' as time));
insert into temp (timestampfield) VALUES (UNIX_TIMESTAMP(cast('1448377601' as UNSIGNED)));
all insert null into the timestamp field (temp is a real table, not a temporary table, I just named it badly!).
Any help you can give on this would be GREATLY appreciated!
Regards,
BlakeyUK
Make sure that timestampfield is a datetime column, then you could use FROM_UNIXTIME function:
insert into temp (timestampfield) values (from_unixtime('1448377601'))
please see a fiddle here.
Oh, I'm an idiot. MySQL timestamp is NOT a unix timestamp. What I need is a simple INTEGER field to store that.

Datetime in MySQL

I'm an experience MS-SQL programmer, but new to MySQL.
I create a table:
create table temp
(
Col1 DateTime NOT NULL
)
I try to insert into the table:
insert into temp select '1/1/2014';
I get an error "Incorrect datetime value". Last I check, that was a valid date.
Your date format is not valid when inserted in MySQL. The correct way is to insert it in 'YYYY-MM-DD' or 'YY-MM-DD'
Please see http://dev.mysql.com/doc/refman/5.6/en/date-and-time-literals.html

How to Insert custom date into mysql timestamp field?

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.