Month in MM using Month() in Hive

我怕爱的太早我们不能终老 提交于 2020-01-13 14:57:34

问题


Select * from concat(YEAR(DATE_SUB(MAX(Column_name),60),MONTH(DATE_SUB(MAX(Column_name),60),-01)

The month() yields only single digit for months until September i.e Jan returns 1 instead of 01. Need help in handling this.

I am using this output to feed to another SELECT query using TO_DATE.


回答1:


month() function returns integer, that is why there is no leading zero. You can use lpad(month,2,0) function to format month:

hive> select lpad(month('2017-09-01'),2,0);
OK
09
Time taken: 0.124 seconds, Fetched: 1 row(s)
hive> select lpad(month('2017-10-01'),2,0);
OK
10
Time taken: 0.433 seconds, Fetched: 1 row(s)

Alternatively you can use substr() to extract year and month from the date:

hive> select substr('2017-10-01',1,4) as year, substr('2017-10-01',6,2) as month;
OK
year    month
2017    10

date_sub() function prior to Hive 2.1.0 (HIVE-13248) return type was a String because no Date type existed when the method was created. See here: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF



来源:https://stackoverflow.com/questions/46658193/month-in-mm-using-month-in-hive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!