Support Questions

Find answers, ask questions, and share your expertise

Equivalent function of trunc(month_date,q) in HiveQL

avatar
New Contributor

Hi, 

 

I have a table in Hadoop stores data monthwise , 

eg. 2014-01-01 

2014-02-01 etc , I need to extract quarter from this. For instance, i acheive this with trunc(month_date_column,'q') in SQL (oracle)

Looking for equivalent function or method in hive. Please help ! tried date_trunc, but that is not working . Looks i have to convert the date column to timestamp first and then try this function. Date is stored as string in my table

 

Any help is appreciated !

 

Thanks

gi

1 REPLY 1

avatar
Champion

If you are using Hive 1.3 you can use quarter() built in function. 

Since you are using String data type for date  looks like you can use em . 

 

Reference  - 

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

 

int quarter(date/timestamp/string)	Returns the quarter of the year for a date, timestamp, or string in the range 1 to 4 (as of Hive 1.3.0)
Example: quarter('2015-04-08') = 2.