HQL 日期函数

9 篇文章 0 订阅
8 篇文章 0 订阅

1 current_date():返回当前日期

select current_date;
# 结果:2020-07-03

2 current_timestamp():返回当前时间

select current_timestamp();
# 结果:2020-07-03 12:42:25.725

3 add_months(start_date, num_months):返回开始日期后num_months的日期

select current_date;
# 结果:2020-07-03
select add_months(current_date,5);
# 结果:2020-12-03

4 date_add(start_date, num_days):返回开始日期后的num_days的日期

select current_date;
# 结果:2020-07-03
select date_add(current_date,10);
# 结果:2020-07-13

5 date_sub(start_date, num_days):返回开始日期前num_days的日期

select current_date;
# 结果:2020-07-03
select date_sub(current_date,10);
# 结果:2020-06-23

6 next_day(start_date, day_of_week):以开始日期为起点,返回下一个周几的日期

select current_date;
# 结果:2020-07-03
select next_day(current_date,'firday');

7 dayofmonth(date):返回date日期在当月的第几天

select current_date;
# 结果:2020-07-03
select dayofmonth(current_date);
# 结果:3

8 weekofyear(date):返回date日期在当年的第几周

select current_date;
# 结果:2020-07-03
select weekofyear(current_date);
# 结果:27

9 minute/hour/day/month/year

select minute(current_timestamp());
# 结果:37

10 date_format(date/timestamp/string, fmt):将日期/时间戳/string转换为日期格式fmt指定格式的字符串值。

select current_date;
# 结果:2020-07-03
select date_format(current_date,'yyyy/MM/dd');

11 datediff(date1, date2):返回date1和date2之间的时间间隔

select current_date;
# 结果:2020-07-03
select datediff(current_date,'2020-03-02');
# 结果:122

12 to_unix_timestamp(date[, pattern]):返回UNIX时间戳

select current_date;
# 结果:2020-07-03
select to_unix_timestamp(current_date);
# 结果:1593705600

13 from_unixtime(unix_time, format):使用指定格式返回UNIX时间

select from_unixtime();
```
14 to_date(datetime):将字符串转化为日期格式
```sql
select to_date('2020-01-02 11:11:11');
# 结果:2020-01-02
```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值