How to store a time_t field to MySQL datetime? - mysql

I have extracted the modification time of a file using the struct stat structure:
long modtime = image_stats.st_mtime;
This returns 1508240128.
Now, I wish to store this value into a MySQL table which has datatype as datetime.
If I store it directly, it fails saying it is not a datetime format.
How do I store it?

You can use FROM_UNIXTIME to convert a timestamp into a DATETIME
Query
SELECT FROM_UNIXTIME(1508240128);
Result
FROM_UNIXTIME(1508240128)
---------------------------
2017-10-17 13:35:28
as insert query
Query
INSERT INTO
[table]
(datetime_column)
VALUES
(FROM_UNIXTIME(1508240128))

Related

Excel Int to date in Hive

I ingested csv table to a Hive table but the date is being shown as integer value. Is there a way to convert Integer value (stored as string) to date in Hive?
When I do this
select cast(day_id2 as date) from table1
...I get null values:
Can someone tell me an elegant way to convert integer values (stored as string) into date values.

HQL return ISO Timestamp

We have Hive table something like below:
CREATE TABLE IF not EXISTS xxxx
(
`Timestamp` Timestamp
)
ROW format serde 'org.apache.hive.hcatalog.data.JsonSerDe' WITH SERDEPROPERTIES ('timestamp.formats'="yyyy-MM-dd'T'HH:mm:ss.SS'Z',yyyy-MM-dd'T'HH:mm:ss.SSS'Z',yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'")
and json data as follows:
{.....,"TimeStamp":"2016-01-26T13:50:00.422Z",....}
Thanks to SERDEPROPERTIES and timestamp.formats hive is able to process this json data into Timestamp format. This is great because we can work with the string timestamp as it was Timestamp so we can use Timestamp's UDFs.
But we would like to also deserialize this data into ISO time format - back to the original one -"2016-01-26T13:50:00.422Z". With that said we need to do:
SELECT * FROM xxx
and get time in ISO format:
2016-01-26T13:50:00.422Z
We would like to avoid using someUDF(Timestamp) in HQL b/c we need to return all fields (using asterix for that). Is it even possible to do such a thing?
This will convert epoch back to iso format but some accuracy in the fractional seconds is lost. Hope it helps. Thanks.
Query: select concat(regexp_replace(cast(from_unixtime(cast(946713612 as bigint),
'yyyy-MM-dd HH:mm:ss.SSS') as string),' ','T'),'Z');
Result: 2000-01-01T03:00:12.000Z

Mysql update a datetime field from an existing varchar field

I had to rebuild a table from a text file, and the data import created my datetime field as a string YY/MM/DD hh:mm:ss .. I have tried str_to_date and date functions, but nothing..
Update mydb set new-date-field = str_to_date( old-str-field, format );
Where format was various yy/mm/dd etc formats..
Thanks
If you truely have a text field of the format yy/mm/dd hh:ii:ss then you can do this to convert that string to a DateTime
UPDATE mytable set new_date_field = TIMESTAMP(old_str_field);
To test it first do this
SELECT TIMESTAMP(old-str-field) as new_date_field FROM mytable;
https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_timestamp

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.

MYSQL: Insert a timestamp from a variable to a table

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