sql时间日期转换方法

timestamp格式转日期

select
  to_date('2021-08-15 11:12:00')
结果:2021-08-15

同时,等价于

select
  from_unixtime(unix_timestamp(comment_timestamp), 'yyyy-MM-dd') as p_date
上面的方法是将timestamp格式先转换成常见的时间戳格式,再将时间戳格式转换成常见的日期

yyyyMMdd转yyyy-MM-dd

select 
  concat(substr(p_date,1,4),'-',substr(p_date,5,2),'-',substr(p_date,7,2))

同时,等价于

select
  default.pdate2dt(p_date) --若没有rd做相关支持,推荐使用上面的方法

–其他日期函数

查询当前系统时间(包括毫秒数): current_timestamp;  
查询当月第几天: dayofmonth(current_date);
月末: last_day(current_date)
当月第1天: date_sub(current_date,dayofmonth(current_date)-1)
下个月第1天: add_months(date_sub(current_date,dayofmonth(current_date)-1),1)
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐