mysql、hive、impal、oracle(时间函数)
官方文档https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
求周一
以下sql $time代表实际业务中的时间值。实现逻辑为:
- 先将$time 转换为char 类型
- 使用拼接函数再将$time转化成date格式——‘YYYY-MM-DD’
- str_to_date处理已经转为日期格式的数据开始运算
- %w返回当前日期是本周周几的编号,返回值是int(0-6) 0 代表周日 6代表周六 如果是周四则返回4
- substr处理转化后的数据,表达式1减去表达式2(int 值)减一。结果为周一日期
select subdate(str_to_date(concat(
substr(cast($time as char),1,4)
,'-'
,substr(cast($time as char),5,2)
,'-'
,substr(cast($time as char),7,2)),'%Y-%m-%d'),date_format(str_to_date(concat(
substr(cast($time as char),1,4