从日期提取日和月时,如何保持前导零

nom7f22z  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(319)

我的约会看起来很有趣

2017-02-22
2017-02-23
2017-02-05

我需要以这种格式提取日期dd.mm,所以看起来

23.02
22.02
05.02

但是这个

concat(day(PLAN_REPAYMENT_DATE_KEY), '.', month(PLAN_REPAYMENT_DATE_KEY))

退货

23.2
22.2
5.2

如何保留前导零?

ffvjumwh

ffvjumwh1#

concat(LPAD(day(PLAN_REPAYMENT_DATE_KEY), 2, '0'), '.', LPAD(MONTH(PLAN_REPAYMENT_DATE_KEY), 2, '0'))

编辑:也在当天添加了lpad。

gc0ot86w

gc0ot86w2#

1. date_format(PLAN_REPAYMENT_DATE_KEY,'dd.MM') 
2. from_unixtime(unix_timestamp(PLAN_REPAYMENT_DATE_KEY),'dd.MM')
3. printf('%02d.%02d',day(PLAN_REPAYMENT_DATE_KEY),month(PLAN_REPAYMENT_DATE_KEY))
hive> with t as (select date '2017-02-05' as PLAN_REPAYMENT_DATE_KEY)
    > select date_format(PLAN_REPAYMENT_DATE_KEY,'dd.MM') from t;
OK
05.02
hive> with t as (select date '2017-02-05' as PLAN_REPAYMENT_DATE_KEY) 
    > select from_unixtime(unix_timestamp(PLAN_REPAYMENT_DATE_KEY),'dd.MM') from t;
OK
05.02
hive> with t as (select date '2017-02-05' as PLAN_REPAYMENT_DATE_KEY)
    > select printf('%02d.%02d',day(PLAN_REPAYMENT_DATE_KEY),month(PLAN_REPAYMENT_DATE_KEY)) from t;
OK
05.02

相关问题