Date Functions in Apache Hive

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
..



