Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive Query to get records between two dates of string type

Tags:

hive

I am trying to query a table with the fields st_date and end_date on Hue. These fields take in string type values e.g. '2014-04-04', '2009-10-10' etc. Suppose, I want to find records between st_date = 2014-04-04' and end_date = '2014-10-10' with both the dates included:

How would I write a query to retrieve records WHERE st_date > 2014-04-03 and end_date < 2014-10-09?

More specifically, I am facing problems related to the date conversion in this query.

like image 404
activelearner Avatar asked Jun 18 '14 20:06

activelearner


People also ask

How do I get data between two dates in Hive?

datediff function in Hive takes 2 dates in String type and gives you the difference between the dates.

How do I query a date in Hive?

Hive Date and Timestamp functions are used to manipulate Date and Time on HiveQL queries over Hive CLI, Beeline, and many more applications Hive supports. The default date format of Hive is yyyy-MM-dd , and for Timestamp yyyy-MM-dd HH:mm:ss .

What is the data type for date in Hive?

Hive supports two data types for Date/Time-related fields— Timestamp and Date : The Timestamp data type is used to represent a particular time with the date and time value. It supports variable-length encoding of the traditional UNIX timestamp with an optional nanosecond precision. It supports different conversions.

Which function will return the number of days between two dates in Hive?

The DATEDIFF function returns the number of days between the two given dates.


2 Answers

Query

   WHERE st_date > '2014-04-03' and end_date < '2014-10-11' 

should give you desired result because even if it is a sting a it will be compared lexicographically i.e '2014-04-04' will be always greater '2014-04-03'.

I ran it on my sample tables and it works perfectly fine.

like image 175
dpsdce Avatar answered Sep 19 '22 23:09

dpsdce


Try below query.

SELECT *
FROM TABLE
WHERE CAST(TRANSLATE(st_date,"-","") AS BIGINT) > CAST(TRANSLATE("2014-04-03","-","") AS BIGINT)  AND CAST(TRANSLATE(end_date,"-","") AS BIGINT) < CAST(TRANSLATE("2014-10-09","-","") AS BIGINT)
like image 36
srinivasan Hariharan Avatar answered Sep 22 '22 23:09

srinivasan Hariharan