I have a timestamp value like "Nov 27, 2016 8:30:00 AM" which I want to convert TIMESTAMP(6) format i.e. YYYY-MM-DD HH:MM:SS.fffffffff
Can anyone suggest how easily can this be achieved in hiveQL.
Use the unix_timsestamp function to get the date
hive> Select unix_timestamp("NOV 27, 2017", "MMM DD, YYYY") from xyz;
OK
1483257600
Time taken: 0.082 seconds, Fetched: 1 row(s)
hive> Select from_unixtime(unix_timestamp("NOV 27,2016 8:30:00 AM", "MMM dd,yyyy HH:mm:ss aa")) from xyz;
OK
2016-11-27 08:30:00
Time taken: 0.075 seconds, Fetched: 1 row(s)
Other options for unix_timestamp can be found here
Related
I'm trying to convert a string with a specific timezone (not UTC) to an unix timestamp.
Conversion from strings in UTC works pretty well with unix_timestamp function:
hive> select unix_timestamp("2018-12-31 23:59:59 UTC", "yyyy-MM-dd HH:mm:ss z") as unixtime;
unixtime
1546300799
However, when I simply change the timezone (to another valid TZ name) it doesn't work:
hive> select unix_timestamp("2018-12-31 23:59:59 America/Sao_Paulo", "yyyy-MM-dd HH:mm:ss z") as unixtime;
unixtime
NULL
Any suggestions?
Your problem is the timezone itself, my understanding is that there is no direct way to do this using java cannonical id ("America/Sao_Paulo"). The correct format for the date that you are looking for (in full) should be
select unix_timestamp("2018-12-31 23:59:59 Brasilia Summer Time", "yyyy-MM-dd HH:mm:ss zzzz") as unixtime;
I have date stored in format DD/MM/YYYY HH24:MI TZ (datetime with timezone) in my psql database. I need to convert this date to a timestamp.
I tried to_timestamp() but it is not working with timezone.
ERROR: "TZ"/"tz"/"OF" format patterns are not supported in to_date
I tried ::timestamptz but it consider date to be in format of MM/DD/YYYY HH24:MI TZ
ERROR: date/time field value out of range
Is there any way to convert the format from DD/MM/YYYY HH24:MI TZ to MM/DD/YYYY HH24:MI TZ or convert DD/MM/YYYY HH24:MI TZ to timestamp?
e.g. "28/04/2017 13:00 +2:30"
try casting directly?..
so=# select to_timestamp('04/28/2017 13:00 +2:30','D/MM/YYYY HH24:MI TZ');
ERROR: "TZ"/"tz"/"OF" format patterns are not supported in to_date
Time: 20.850 ms
so=# select '04/28/2017 13:00 +2:30'::timestamptz;
timestamptz
------------------------
2017-04-28 10:30:00+00
(1 row)
Time: 0.554 ms
https://www.postgresql.org/docs/current/static/functions-formatting.html
to_timestamp and to_date exist to handle input formats that cannot be
converted by simple casting. For most standard date/time formats,
simply casting the source string to the required data type works, and
is much easier.
update
regarding your comment, you just need to adjust datestyle to change the parsing:
so=# set datestyle to DMY;
SET
Time: 9.997 ms
so=# select '04/28/2017 13:00 +2:30'::timestamptz;
ERROR: date/time field value out of range: "04/28/2017 13:00 +2:30"
LINE 1: select '04/28/2017 13:00 +2:30'::timestamptz;
^
HINT: Perhaps you need a different "datestyle" setting.
Time: 10.217 ms
so=# set datestyle to MDY;
SET
Time: 8.799 ms
so=# select '04/28/2017 13:00 +2:30'::timestamptz;
timestamptz
------------------------
2017-04-28 10:30:00+00
(1 row)
in hive sql I have the following field as date time
date_time
2017-01-01 12:00:00
min_date
2017-02-01 12:00:00
can I compare both fields as date_time > min_date
in my sql query?
how do we compare date time in hive sql?
both timestamp types
You can compare timestamps or strings if they are in sort-able format like this yyyy-MM-dd HH:mm:ss[.f...]
Demo:
hive> select cast('2017-01-01 12:00:00' as timestamp)>cast('2017-02-01 12:00:00' as timestamp);
OK
false
Time taken: 0.13 seconds, Fetched: 1 row(s)
Example with strings:
hive> select '2017-01-01 12:00:00'>'2017-02-01 12:00:00';
OK
false
Time taken: 1.053 seconds, Fetched: 1 row(s)
I have date in table as Sep 1 2017 2:00 PM as actualshipdate I want to convert it as 01-09-2017 in hive I try with below command but is showing null select actualshipdate,from_unixtime(unix_timestamp(substr(actualshipdate,0,11), 'dd-mm-yyyy')) as newdate from tablename;
Use unix_timestamp(string date, string pattern) to convert given date format to seconds passed from 1970-01-01. Then use from_unixtime() to convert to given format:
hive> select from_unixtime(unix_timestamp('Sep 1 2017 2:00 PM' ,'MMM dd yyyy HH:mm a'), 'dd-MM-yyyy');
OK
01-09-2017
Time taken: 0.049 seconds, Fetched: 1 row(s)
See patterns examples here: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You are using correct function but the parameters are wrong . The parameters should be unix_timestamp(datestring, date_format) ; this function will convert date to unix date format which you can format further by using from_unixtime(unixdateformat,format_youneed);
hive> select unix_timestamp('Sep 1 2017 2:00 PM' ,'MMM dd yyyy HH:mm a');
OK
1504256400
You need specific pattern of date for this you can use function
from_unixtime(unixdateformat,format_youneed);
hive> select from_unixtime(1504256400,'dd-MM-yyyy');
OK
01-09-2017
hive> select from_unixtime(UNIX_TIMESTAMP('Sep 1 2017 2:00 PM','MMM dd yyyy
HH:mm a'), 'dd-MM-yyyy');
OK
01-09-2017
Time taken: 0.135 seconds, Fetched: 1 row(s)
As you have date stored in actualdate in the table you can use below command to get the result.
**hive> select from_unixtime(UNIX_TIMESTAMP(actualshipdate,'MMM dd yyyy HH:mm a'), 'dd-MM-yyyy') from tablename;**
Time is visible on my table as follows
2017-09-15 16:30:00.000
And my Query is like this
Format(SlotStartTime,'dd/MM/yyyy hh:mi:ss') as SlotStartTime
And this returns time like this
15/09/2017 04:30:00
So the calculation gets wrong it should be 15/09/2017 04:30:00 PM or stay as it is in 24-hour format. How can I achieve this?
hh gives you 12 hour hours
HH gives you 24 hour hours
Also, mi is probably a typo, as it will return you a minute, followed by the letter i. Try this:
declare #datetime datetime = '2017-08-29 16:30:01'
select Format(#datetime,'dd/MM/yyyy HH:mm:ss') as SlotStartTime
You can find all valid datetime formatting characters here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
hh is the code for 12 hour format. HH is the code for 24 hour format. It's similar to how mm (not mi) is for minutes and MM is for months.
You want:
FORMAT(SlotStartTime,'dd/MM/yyyy HH:mm:ss')
12 hour with the time period:
FORMAT(SlotStartTime,'dd/MM/yyyy hh:mm:ss tt')