HQL 日期函数

    技术2023-05-27  36

    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 ```
    Processed: 0.018, SQL: 8