oracle函数记录-trunc日期格式ww、iw、w的区别

trunc日期格式ww、iw、w的区别


wwSame day of the week as the first day of the year 
按年度1月1日的第一天为每周第一天
iwSame day of the week as the first day of the ISO week, which is Monday
 每周一
wSame day of the week as the first day of the month
 按月份1日的第一天作为每周第一天


ww,按每年的1月1日为第一周第一天,1月8日为第二周第一天……依此类推


w,与ww功能类似,不过它是按每月的1日为每周第一天,例如1日为当月第一周第一天,8日为第二周第一天……依此类推。

也就是说,1月份的参数w与ww用trunc函数的结果应该是一致的。


2017-1-3为2017年第一周第三天,这周第一天为2017-1-1

select trunc(date'2017-1-3','w') from dual;

-------------------------
2017-1-1


2017-1-10为2017年第二周第三天,这周第一天为2017-1-8
select trunc(date'2017-1-10','w') from dual;

-------------------------

2017-1-8


iw,不受月份与年份的影响,直接取日期参数的周一。

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Python 中,可以使用 `datetime` 模块来实现类似 Oracle 的 `TRUNC` 函数来截取日期。下面是一个示例代码: ```python from datetime import datetime def trunc_date(date, trunc_level): if trunc_level == 'YEAR': return date.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) elif trunc_level == 'MONTH': return date.replace(day=1, hour=0, minute=0, second=0, microsecond=0) elif trunc_level == 'DAY': return date.replace(hour=0, minute=0, second=0, microsecond=0) elif trunc_level == 'HOUR': return date.replace(minute=0, second=0, microsecond=0) elif trunc_level == 'MINUTE': return date.replace(second=0, microsecond=0) elif trunc_level == 'SECOND': return date.replace(microsecond=0) else: raise ValueError('Invalid truncation level') # 示例用法 date = datetime(2022, 10, 15, 13, 45, 30, 123456) truncated_date = trunc_date(date, 'MONTH') print(truncated_date) # 输出:2022-10-01 00:00:00 ``` 以上代码定义了一个 `trunc_date` 函数,接受一个日期对象和截取级别作为参数。根据截取级别,利用 `replace` 方法来修改日期对象的对应部分,实现截取效果。 在示例中,我们给定了一个日期时间对象 `date`,然后调用 `trunc_date` 函数来截取到月份级别,最后打印截取后的日期对象 `truncated_date`。输出结果为 `2022-10-01 00:00:00`,只保留了年份和月份部分,其它部分被置为零。 你可以根据需要调整 `trunc_date` 函数中的截取级别并进行测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值