How to convert date - hive

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;**

Related

varchar2 string to Date format

I am trying to convert the below string value to TO_DATE but oracle is not
recognizing the date format. Where am I going wrong.
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC','Dy Mon DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE = American')
FROM dual;
ERROR at line 1:
ORA-01821: date format not recognized
Use:
SELECT TO_TIMESTAMP_TZ('Wed Oct 10 23:50:00 2018 UTC','DY MON DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE=American')
FROM dual;
TO_DATE function returns DATE datatype, which does not support timezones. You are using TZR format specifier in your query (time zone region), and this generates the error because DATE does not support this.
This documentation - Time Zones shows which datatypes supports timezones, and states that only TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE do. So you must convert to one of this datatype instead of converting to DATE.
TO_TIMESTAMP_TZ function converts a literal to TIMESTAMP WITH TIME ZONE datatype.
If (and I realise it's a big 'if') the string values always contain UTC and not any other time zone values, then you could just treat that as a character literal. You would do that by changing your format model from TZR, which isn't recognised by to_date(), to "UTC" - including the double quotes:
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC',
'Dy Mon DD HH24:MI:SS YYYY "UTC"',
'NLS_DATE_LANGUAGE = American')
FROM dual;
TO_DATE('WEDOCT1023
-------------------
2018-10-10 23:50:00
Of course, as that is a plain date it still has no time zone information, but if you want to retain that then you need a timestamp with [local] time zone data type anyway.

date time comparisons in hive sql

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)

SQL Automatically converts 24 hour format time to 12 hour format

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')

postgresql - Convert string to time

I have column where I saved the transaction time, format is HHMMSS
for example:
140159
013115
235900
then I want to convert those time to HH:MM AM/PM
so results would be:
2:01 PM
1:31 AM
11:59 PM
Here are the queries ive tried, but none of them return the results I want..
SELECT TO_CHAR(TO_TIMESTAMP(TRANSTIME,'hh24:mi:ss AM'),'hh12:mi:ss AM')
FROM PRODUCTSALES order by TRANSTIME desc LIMIT 100
SELECT TO_TIMESTAMP(TRANSTIME, 'HH24:MI')::TIME
time data type is just time - not a format. to get time with wanted format use to_char, eg fro your 140159:
t=# select to_char('140159'::time,'HH:MI AM');
to_char
----------
02:01 PM
(1 row)
Mind I first cast as time and only then format it

Converting timestamp in hive

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