Skip to main content

Command Palette

Search for a command to run...

Date Functions in Apache Hive

Updated
โ€ข2 min read
Date Functions in Apache Hive
N

I am a Tech Enthusiast having 13+ years of experience in ๐ˆ๐“ as a ๐‚๐จ๐ง๐ฌ๐ฎ๐ฅ๐ญ๐š๐ง๐ญ, ๐‚๐จ๐ซ๐ฉ๐จ๐ซ๐š๐ญ๐ž ๐“๐ซ๐š๐ข๐ง๐ž๐ซ, ๐Œ๐ž๐ง๐ญ๐จ๐ซ, with 12+ years in training and mentoring in ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐“๐ž๐ฌ๐ญ ๐€๐ฎ๐ญ๐จ๐ฆ๐š๐ญ๐ข๐จ๐ง ๐š๐ง๐ ๐ƒ๐š๐ญ๐š ๐’๐œ๐ข๐ž๐ง๐œ๐ž. I have ๐’•๐’“๐’‚๐’Š๐’๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 10,000+ ๐‘ฐ๐‘ป ๐‘ท๐’“๐’๐’‡๐’†๐’”๐’”๐’Š๐’๐’๐’‚๐’๐’” and ๐’„๐’๐’๐’…๐’–๐’„๐’•๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 500+ ๐’•๐’“๐’‚๐’Š๐’๐’Š๐’๐’ˆ ๐’”๐’†๐’”๐’”๐’Š๐’๐’๐’” in the areas of ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐ƒ๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ฆ๐ž๐ง๐ญ, ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐‚๐ฅ๐จ๐ฎ๐, ๐ƒ๐š๐ญ๐š ๐€๐ง๐š๐ฅ๐ฒ๐ฌ๐ข๐ฌ, ๐ƒ๐š๐ญ๐š ๐•๐ข๐ฌ๐ฎ๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง๐ฌ, ๐€๐ซ๐ญ๐ข๐Ÿ๐ข๐œ๐ข๐š๐ฅ ๐ˆ๐ง๐ญ๐ž๐ฅ๐ฅ๐ข๐ ๐ž๐ง๐œ๐ž ๐š๐ง๐ ๐Œ๐š๐œ๐ก๐ข๐ง๐ž ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐ . I am interested in ๐ฐ๐ซ๐ข๐ญ๐ข๐ง๐  ๐›๐ฅ๐จ๐ ๐ฌ, ๐ฌ๐ก๐š๐ซ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ค๐ง๐จ๐ฐ๐ฅ๐ž๐๐ ๐ž, ๐ฌ๐จ๐ฅ๐ฏ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐ž๐ฌ, ๐ซ๐ž๐š๐๐ข๐ง๐  ๐š๐ง๐ ๐ฅ๐ž๐š๐ซ๐ง๐ข๐ง๐  new subjects.

One of the most important column types is the date/time in the data. The date/time helps in understanding the patterns, trends and even business.

For test data, use the following commands in Apache Impala.

hive> CREATE TABLE test_tbl(date1 TIMESTAMP);
hive> INSERT INTO test_tbl VALUES(โ€˜2023-01-23 19:14:20.000โ€ฒ);

Year(string date)

The year function returns the year from the given input.

hive> SELECT YEAR(date1) FROM test_tbl;
OK
2023

Month(string date)

The month function returns the month of the given input.

hive> SELECT MONTH(date1) FROM test_tbl;
1

MINUTE(string date):

The Minute function returns the minute of the given input.

hive> SELECT MINUTE(date1) FROM test_tbl;
14

SECOND(string date):

The second function returns the second of the given input.

hive> SELECT SECOND(date1) FROM test_tbl;
20

DAY(string date)

The day function returns the day of the given input.

hive> SELECT day(date1), day(date2) FROM test_tbl;
23

DayOfMonth

The dayofMonth function returns the day of the given input.

hive> SELECT dayofmonth(date1) FROM test_tbl;

HOUR(string date)

The hour function returns the hour of the given input.

hive> SELECT hour(date1) FROM test_tbl;

WeekofYear(string date)

The weekofyear function returns the week of the given input.

hive> SELECT weekofyear(date1) FROM test_tbl; 
3

Current_Date

This function returns the current date (without timestamp).

hive> SELECT CURRENT_DATE();
2023-04-12

Current_Timestamp:

This function returns the current date and time.

hive> SELECT current_timestamp();
2023-04-12 09:00:20.002

Extract

As per the documentation, this function helps in retrieving fields such as days or hours from source. Source must be a date, timestamp, interval or a string that can be converted into either a date or timestamp. Supported fields include: day, dayofweek, hour, minute, month, quarter, second, week and year. This function introduced in Hive 2.2.0

hive> SELECT EXTRACT(month F โ€œ2023-10-20โ€) 
10
hive> SELECT EXTRACT(hour from โ€œ2023-10-20 05:06:07โ€) 
5
hive> SELECT EXTRACT(dayofweek from โ€œ2023-10-20 05:06:07โ€)
5
hive> SELECT EXTRACT(month from interval โ€˜1-3โ€™ year to month)
3
hive> SELECT EXTRACT(minute from interval โ€˜3 12:20:30โ€™ day to second)
20

..

More from this blog

Naveen P.N's Tech Blog

94 posts