Naveen
Naveen

Reputation: 791

convert date format in hive

How can I convert this date :

Dec 30, 2013

in the format 'YYYY-MM-DD' in hive :

2013-12-30

I can do explode and concat to match with given format but converting Dec to 12 is problem.

Upvotes: 0

Views: 4913

Answers (2)

Raunak Jhawar
Raunak Jhawar

Reputation: 1651

The UDF is the way to go in hive or impala, but if you have an ETL defined before loading data in HDFS, suggest let the ETL carry out the date conversion. It is important to store dates in format YYYY-MM-DD in order to leverage the TO_DATE() in hive or impala

Upvotes: 1

Mukesh S
Mukesh S

Reputation: 2876

If all your date is in this format then you can write a UDF which reads that column as string, then converts the date to a given format. Something like this:

public String convertDate(String dt2) throws ParseException{
    SimpleDateFormat incommingDateFormat = new SimpleDateFormat("MMM dd, yyyy");
    SimpleDateFormat convertedDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date dt = incommingDateFormat.parse(dt2);
    return convertedDateFormat.format(dt);
}

Hope this helps...!!!

Upvotes: 0

Related Questions