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
Related
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))
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)
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
A very simple Insert statement with a topic and a date I would like to insert the date as a certain format "MM/DD/YY" Don't know if it's possible or not?
$sql = "INSERT INTO Topic (Topic,date) VALUES ('$Topic',NOW())";
No, it is not possible using just NOW(). That format is not the standard date format for MySQL so you have to store it as a string. But that is a bad idea because you can use any of MySQL's date functions without first converting back into a valid date which is tedious.
Just store your dates in the date data type and then when you want to retrieve them use DATE_FORMAT() to transform them into MM/DD/YY format.
SELECT DATE_FORMAT(date, "%m/%d/%y") as date FROM ....
-- date is now in MM/DD/YY format
I have been researching this for a week and still no luck. I have a txt file that loads into 'temploadsi' table via php. The text file will always have old data that is already in the database so I only want the "new" data. My idea was to have a loading table 'temploadsi' and then compare the time stamps and import only "new" data into table 'tempdatasi'. The issue is that the time stamp column is formatted like 'MM/DD/YYYY HH:MM:SS' and that will not go into the mySQL datetime field. Therefore I have it set as a text field. I would like to convert the text into a datetime field like 'YYYY-MM-DD HH:MM:SS'. I have looked at STR_TO_DATE and DateTime functions but unable to update 'temploadsi'. Can you all help out?
The reason why you failing to update the field is because the time of your data is 24:59:59 which is not valid. The maximum time i think is 23:59:59, so in order to convert that to date, you need to use the following format
SELECT DATE_FORMAT(STR_TO_DATE(datetime,'%m/%d/%Y %x:%x:%x'), '%Y-%m-%d 23:59:59')
FROM table1
See SQLFiddle Live Demo
So if you wnt to update your field, you can use this query
UPDATE tableName
SET your_columnname = DATE_FORMAT(STR_TO_DATE(your_columnname, '%m/%d/%Y %x:%x:%x'), '%Y-%m-%d 23:59:59')
and this query assumes that your column you are updating has VARCHAR datatype.
Something like Str_to_date('12/10/2012 23:59:59','%m/%d/%y %h:%i:%s')
Don't get why you couldn't find this though
[Mysql Help]
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date