Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

Hive - why doesn't truncate support a DD/Day value?

avatar

From the https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions

trunc(string date, string format)

I'm executing these commands in beeline:

0: jdbc:hive2://> select trunc(current_timestamp(), 'MM');
OK
+-------------+--+
|     _c0     |
+-------------+--+
| 2016-01-01  |
+-------------+--+
1 row selected (0.97 seconds)
0: jdbc:hive2://> select trunc(current_timestamp(), 'DD');
OK
+-------+--+
|  _c0  |
+-------+--+
| NULL  |
+-------+--+
1 row selected (0.241 seconds)

Now, the question is why doesn't it support a day value? This is such a natural function for truncating timestamps to a day only.

1 ACCEPTED SOLUTION

avatar

Through experiments I found this one works as expected:

jdbc:hive2://> select date_sub(current_timestamp(), 0);
OK
+-------------+--+
|     _c0     |
+-------------+--+
| 2016-01-05  |
+-------------+--+

Which is much harder than it should be. Given how much legacy SQL there is relying on trunc(timestamp), can we enhance the Hive's function to throw the timestamp out too maybe?

View solution in original post

3 REPLIES 3

avatar

Through experiments I found this one works as expected:

jdbc:hive2://> select date_sub(current_timestamp(), 0);
OK
+-------------+--+
|     _c0     |
+-------------+--+
| 2016-01-05  |
+-------------+--+

Which is much harder than it should be. Given how much legacy SQL there is relying on trunc(timestamp), can we enhance the Hive's function to throw the timestamp out too maybe?

avatar

I found that to_date() function generates correct data types which will work correctly with comparisons and ordering.

avatar
New Member
Hi there,
I was able to get this to work in my select but not in my where clause - any advice?  Thank you in advance!
 
Select
date_sub(start_time, 0) ,
anchor_id
From a.b
where
date = max_pt(a.b)
and date_sub(start_time, 0) >= '${date-28}'