I am trying to convert string '2022-12-28T22:28:43.260781049Z' to datetime format.
I have such query:
SELECT date(str_to_date('2022-12-28T22:28:43.260781049Z','%Y-%m-%d')) as date,
hour(str_to_date('2022-12-28T22:28:43.260781049Z',"%H:%M:%S")) as hour
FROM transaction
And such output:
date
time
'2022-12-28'
NULL
How to get time as well?
You can directly use a CAST on your string value to TIMESTAMP, then extract the date and the time with the hononimous DATE and TIME MySQL functions.
SELECT DATE(CAST(timestamp_ AS DATETIME)) AS date_,
TIME(CAST(timestamp_ AS DATETIME)) AS time_
FROM transactions;
Check the demo here.
Use timestamp instead of str_to_date:
SELECT hour(timestamp('2022-12-28T22:28:43.260781049Z')) as hour
Related
I am writing the code in java which use DATE columns from mySQL database.
I would like to get average from DATE column but:
-avg(date_col) returns not date yyyy-mm-dd but some kind "weird" number,
I read a lot of stack topics "get average from date" but everyone uses timestamp or datetime (not date)... and all i tried were not working for me.
To sum up...
I have to get average from DATE column, which returns me "yyyy-mm-dd" because my program is parsing it in the following etap.
How can i get it?
Convert the date to unixtime, take the average and convert back to datetime:
select cast(from_unixtime(avg(unix_timestamp(dt))) as date)
from data;
Unixtime is simply seconds elapsed from 1970-01-01, so it is an integer where you can calculate the average from.
DATE_FORMAT with UNIX-Timestamp will do the trick
SELECT DATE_FORMAT(FROM_UNIXTIME(AVG(UNIX_TIMESTAMP(dt))) , '%Y-%m-%d')
FROM data;
Thsi convert time intp 2019-11-30 .
But there are more Format options
select DATE_FORMAT('8:48:30 AM', '%H:%i:%s')
It returns Null why ?
but when using
select DATE_FORMAT(CURTIME(), '%H:%i:%s')
It return formatted value.
It's returning NULL because MySQL isn't successfully parsing the string into a valid DATETIME value.
To fix the problem, use the STR_TO_DATE function to parse the string into a TIME value,
SELECT STR_TO_DATE('8:48:30 AM', '%h:%i:%s %p')
Then, to get the TIME value converted to a string in a particular format, use the TIME_FORMAT function, e.g. 24-hour clock representation:
SELECT TIME_FORMAT(STR_TO_DATE( '8:48:30 AM', '%h:%i:%s %p'),'%H:%i:%s')
returns:
--------
08:48:30
The method DATE_FORMAT is used to display date and time, however in the first you are not assigning any date except time, so its is throwing null.
From the manuals -
DATE_FORMAT Formats the date value according to the format string.
In MySql version 5.5 SELECT DATE_FORMAT( CURTIME( ) , '%H:%i:%s' ) returns null
DATE_FORMAT 's first parameter is of type DATETIME. On recent mysql server versions both your queries return NULL.
So the answer to your question is that this difference in behaviour is because of a bug in your mysql version - in some way it converts the TIME to DATETIME, while it cannot convert the string to DATETIME.
Here is also an example of a working query:
select DATE_FORMAT(NOW(), '%H:%i:%s')
NOW() returns a DATETIME while CURTIME() returns TIME.
To my knowledge, I think it's because MySQL recognises the function as a time, and therefore knows how to handle it. Whereas, in the first example, it regards it as a string and doesn't know what to do with it.
I have a log file which contains timestamp column. The timestamp is in unix epoch time format.
I want to create a partition based on a timestamp with partitions year, month and day.
So far I have done this but it is throwing an error.
PARSE ERROR cannot recognize input '(' in column type
Here is my code.
from (
from raw_data
MAP ${PREFIX}raw_data.line
USING 's3://scripts/clean.py'
AS (timestamp STRING, name STRING)
) map_out
INSERT OVERWRITE TABLE date_base_data_temp PARTITION(year(timestamp), month(timestamp)), day(timestamp)))
select map_out.name;
Oof, that looks ugly. Try using this function in Hive:
SELECT from_unixtime(unix_timestamp) as new_timestamp from raw_data ...
Or if timestamp is in ms instead of seconds:
SELECT from_unixtime(unix_timestamp DIV 1000) as new_timestamp from raw_data ...
That converts a unix timestamp into a YYYY-MM-DD HH:MM:SS format, then you can use the following functions to get the year, month, and day:
SELECT year(new_timestamp) as year, month(new_timestamp) as month, day(new_timestamp) as day ...
With more recent releases of Hive and SparkSQL, data type of date and type casting options are available. Following should work in Hive as well as Spark SQL
SELECT cast(from_unixtime(epoch_datetime) as date) from myHiveTable
If you need to convert the date in custom format, use this:
select date_format(from_unixtime(epoch_datetime),'yyyyMM') as formatted_date from myHiveTable;
which will return the date as yearMonth e.g. 201708
Adding this query to the list where the timestamp needs to be converted to date string yyyy-MM-dd for a string partition:
hive> select date_format(from_unixtime(epoch_datetime), 'yyyy-MM-dd') as day from table_name limit 20;
-- If required, remove the millis precision for timestamps
hive> select date_format(from_unixtime(cast(epoch_datetime/1000 as bigint)), 'yyyy-MM-dd') as day from table_name limit 20;
select order_id, date_format(from_unixtime(order_date/1000),'yyy-MM-dd') as order_date ,order_customer_id,order_status
from orders
or if you see any error on the same , try to use
select order_id, date_format(from_unixtime(order_date DIV 1000),'yyy-MM-dd') as order_date ,order_customer_id,order_status
from orders
I have a problem: I have a datetime and I need the date to specific format
So I just casted datetime to time
SELECT CAST (GETDATE() AS DATE) -- result (2011-06-08)
and for formatting I use convert
SELECT CONVERT(DATE, CAST (GETDATE() AS DATE), 105) --result (2011-06-08)
105 format (dd-mm-yy)
but, the result of both is same,
CONVERT is not working for 105 formatting,
Any ideas?
thanks
To get the results you're looking for you need to convert the DATE to a VARCHAR like this:
SELECT CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105)
If you cast to a DATE, you will always get the full DATE.
You can truncate the date by re-casting to the DATE type.
SELECT CAST(CONVERT(VARCHAR(10),CAST (GETDATE() AS DATE),105) as DATE)
You can use as
SELECT Convert(varchar, getdate(), 105)
select convert(varchar(50),date,105) as Date
Is there way in MySQL to create DATETIME from a given attribute of type DATE and a given attribute of type TIME?
Copied from the MySQL Documentation:
TIMESTAMP(expr), TIMESTAMP(expr1,expr2)
With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.
mysql> SELECT TIMESTAMP('2003-12-31');
-> '2003-12-31 00:00:00'
mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
-> '2004-01-01 00:00:00'
To get a true DATETIME value from your two separate DATE and TIME values:
STR_TO_DATE(CONCAT(date, ' ', time), '%Y-%m-%d %H:%i:%s')
You could use ADDTIME():
ADDTIME(CONVERT(date, DATETIME), time)
date may be a date string or a DATE object.
time may be a time string or a TIME object.
Tested in MySQL 5.5.
datetime = CONCAT(date, ' ', time);
select timestamp('2003-12-31 12:00:00','12:00:00');
works, when the string is formatted correctly. Otherwise, you can just include the time using str_to_date.
select str_to_date('12/31/2003 14:59','%m/%d/%Y %H:%i');
Without creating and parsing strings, just add an interval to the date:
set #dt_text = '1964-05-13 15:34:05.757' ;
set #d = date(#dt_text) ;
set #t = time(#dt_text) ;
select #d, #t, #d + interval time_to_sec( #t ) second;
However this truncates the microseconds.
I agree with Muki - be sure to take account of time zones and daylight savings time!