EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Hive Tutorial Date Functions in Hive
 

Date Functions in Hive

Priya Pedamkar
Article byPriya Pedamkar
Quick Video Summary
EDUCBA Youtube Shorts

Updated March 28, 2023

date function in hive

 

 

Introduction to Date Functions in Hive

Hive provides Date Functions that help us in performing different operations on date and date data types. Hive has a wide variety of built-in date functions similar. Date functions in Hive are almost like date functions in RDBMS SQL. Date functions are used for processing and manipulating data types. In this article, we will discuss various Date Functions provided by Hive in detail with multiple examples.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

Data Types in Hive are very complicated and highly formatted too. Every date value holds multiple information like a second, minute, hour, day, week, month, year, century, etc., all these date data can be manipulated using Hive built-in date functions.

  • current_date(): This function returns the current system date alone. It does not include the time part.
  • current_timestamp(): This function returns the current system time and date in a complete format.
  • unix_timestamp(): This function uses the default time zone of the Unix epoch and returns a converted time format of the number of seconds between the Unix epoch and the specified date in string format.
  • unix_timestamp(str date): This function is used to convert  ‘yyyy-MM-dd HH:mm: ss’ date format into normal Unix timestamp. It returns a converted time format of the number of seconds between the Unix epoch and the specified date in string format. And, it returns 0 on failure.
  • unix_timestamp(str date, str pattern): This function is used to convert normal data type into another data type which can be specified in string pattern. It returns a converted time format of the number of seconds between the Unix epoch and the specified date in string format. And, it returns 0 on failure.
  • from_unixtime(bigint number of seconds  [, str format]): This function, from Unix epoch, converts the given number of seconds and returns the date in ‘yyyy-MM-dd HH:mm: ss’ format.
  • from_utc_timestamp(str date, time zone): This function converts the specified date format in string format to the specified time zone in the second part of the expression. This function is used for the time zone conversion of the UTC time zone to another time zone.
  • to_utc_timestamp(str date, time zone): This function converts the specified data format in a string format in the time zone specified in the second part to the UTC time zone. This function is used for the time zone conversion of another time zone to the UTC time zone.
  • to_date(str timestamp): This function is used to return only the date part of the specified timestamp in standard date format ‘yyyy-MM-dd’.
  • date_add(str date, int number of days): This function is used to add the specified number of days to the given date and return the final added date.
  • date_sub(str date, int number of days): This function is used to subtract the specified number of days from the given date and return the final subtracted date.
  • date_diff(str date 1, str date 2): This function is used to find the difference between two specified dates and returns the difference in the number of days.
  • Year (str date): This function is used to return the year portion of the given date in string format.
  • Quarter (str date): This function is used to return the year portion of the given date in string format.
  • Month (str date): This function is used to return the month portion of the given date in string format.
  • Day (str date): This function is used to return the day portion of the given date in string format.
  • dayofmonth (timestamp or date or string): This function is used to return the quarter number of the given date in string format.
  • Hour (str date): This function is used to return the given date’s hour portion in string format.
  • Minute (str date): This function is used to return the minute portion of the given date in string format.
  • Second (str date): This function is used to return the second portion of the given date in string format.
  • weekofyear(str date): This function is used to return the week number of the given date in string format.
  • trunc(timestamp, str unit): This function is used to strip off all the given timestamp fields in string format.
  • last_day(str date): This function is used to return the last day of the specified month in the given date in string format.
  • next_day(str start date, str day of the week): This function is used to return the next day of the week from the given start date.
  • months_between(str date 1, str date 2): This function returns the number of months between the given dates.
  • date_format(timestamp or date or string, str format): This function is used to convert the specified date in any specified to the given format.

How Does Date Function work in Hive?

  • Hive does not have date data types.
  • Dates in Hive are considered as normal strings.
  • Hive provides this wide variety of Date Functions for working, manipulating, and processing of date data types as per the requirements.
  • Date data type handling in Hive is more difficult as compared to RDBMS.
  • Hive is not able to throw an effective error when dealing with improper date formats.
  • Any format incompatible date formats will result in NULL as output instead of throwing an error in Hive.
  • date_format function can be used to convert any format to the required date format.

Examples to Implement in Date Functions

Below we will learn about the examples to implement in date function:

1. current_date()

Code:

select current_date();

Output:

Date Functions in Hive - 1

2. current_timestamp()

Code:

select current_timestamp();

Output:

Date Functions in Hive - 2

3. unix_timestamp(str date)

Code:

select unix_timestamp('2000-01-01 00:00:00');

Output:

Date Functions in Hive - 3

4. unix_timestamp(str date, str pattern)

Code:

select unix_timestamp('2009-03-20 00:00:00', 'yyyy-MM-dd');

Output:

Date Functions in Hive - 4

5. from_unixtime(bigint number of seconds  [, str format])

Code:

select from_unixtime(unix_timestamp());

Output:

Date Functions in Hive - 5

6. from_utc_timestamp(str date, time zone)

Code:

select from_utc_timestamp('1990-01-01 09:00:00', 'JST');

Output:

Date Functions in Hive - 6

7. to_utc_timestamp(str date, time zone)

Code:

select to_utc_timestamp ('1990-02-13 00:00:00','America/Denver');

Output:

Date Functions in Hive - 7

8. to_date(str timestamp)

Code:

select to_date ('1990-02-13 07:57:33');

Output:

Date Functions in Hive - 8

9. date_add(str date, int number of days)

Code:

select date_add ('2020-02-23', 4);

Output:

Date Functions in Hive - 9

10. date_sub(str date, int number of days)

Code:

select date_sub ('2020-02-23', 4);

Output:

Date Functions in Hive - 10

11. date_diff(str date 1, str date 2)

Code:

select date_add ('2020-02-23', 2020-02-19);

Output:

Date Functions in Hive - 11

12. year(str date)

Code:

select year ('2020-02-23 05:23:36');

Output:

year

13. quarter(str date)

Code:

select quarter ('2020-02-23 05:23:36');

Output:

quarter

14. month(str date)

Code:

select month ('2020-02-23 05:23:36');

Output:

month

15. day(str date)

Code:

Select day('2020-02-23 05:23:36');

Output:

day

16. dayofmonth(timestamp or date or string)

Code:

select dayofmonth ('2020-02-23 05:23:36');

Output:

dayofmonth

17. hour(str date)

Code:

select hour ('2020-02-23 05:23:36');

Output:

hour

18. minute(str date)

Code:

select minute ('2020-02-23 05:23:36');

Output:

minute

19. second(str date)

Code:

select second ('2020-02-23 05:23:36');

Output:

second

20. weekofyear(str date)

Code:

select weekofyear ('2020-01-23 05:23:36');

Output:

weekofyea

21. months_between(str date 1, str date 2)

Code:

select months_between ('2020-01-20’, ‘2019-10-15’);

Output:

 months_between

Recommended Articles

This is a guide to Date Functions in Hive. Here we discuss syntax, how does the date function in hive work with examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –

  1. Views in Hive
  2. Bucketing in Hive
  3. Hive Data Types
  4. Hive String Functions
  5. Guide to PostgreSQL Date Functions

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW