Mysql LONGTEXT With timestamp string to actual TIMESTAMP - mysql

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.

Related

Missing date after importing csv to MySQL using phpmyadmin

CSV date format is DD/MM/YYYY like this 16/11/2016. All the date become 0000-00-00 in MySQL. How to solve this differences?
As of now I can think of two solutions to you problem:
Create a additional VARCHAR field to insert those values (like 16/11/2016), and create a TRIGGER on INSERT to update the date field by converting the string date to 'YYYY-mm-dd' type.
These links may help
https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
http://www.w3resource.com/mysql/date-and-time-functions/mysql-str_to_date-function.php
In this step also, Create a additional VARCHAR field to insert those values (like 16/11/2016) and after import is done run a UPDATE query to update your date field using SET dateField = convertDate(dateFromSheet)

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.

Auto-store datetime() value in select insert query mysql

need an advice, how to auto-store datetime value for my historyActivity table in select insert mysql query. This is an example:
INSERT INTO history_sequence(CODE, LAST_MOUNTH, LAST_VALUE) SELECT CODE, MOUNTH, VALUE FROM seq WHERE CODE = CODEVALUE
i just want to add datetime to see time when the data inserted. Need help please
You can do this in the MySQL table definition:
ALTER TABLE history_sequence ADD inserted TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
When records are inserted into the table table, the inserted column gets automatically populated with the current timestamp.

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

Converting datetime to unix timestamp in 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