trunc和date_trunc的区别

总结

通过对两个函数的测试,发现有如下特点:

  • 针对的字段类型不同:trunc是针对date类型处理,date_trunc是针对time_stamp类型处理;如果输入的时候不是对应的类型,会自动用cast函数处理;
  • 参数的位置:trunc的参数在后面,date_trunc的参数在前面;
  • 仅从个人练习的情况看,trunc的用法比date_trunc少些,因为trunc到date级别,date_trunc到time_stamp级别,所以以后可以多用date_trunc和sub_str基本能够解决绝大部分需求;

trunc

测试结果:在spark_sql中只有到日期的位置可以用trunc函数,即输入可以是date、time_stamp类型,但是输出只能是date类型;

select trunc('2021-12-12 09:12:12', 'yyyy')
 
输出:2021-01-01
 
select trunc(cast('2021-12-12' as date), 'yy')
 
输出:2021-01-01
 
select trunc(cast('2021-12-12' as date), 'mm')
 
输出:2021-12-01
 
select trunc('2021-12-12 09:12:12', 'week')
 
输出:2021-12-06

date_trunc

用法:date_trunc(格式, 日期字段)

说明:返回该时间/日期对应的取断时间/日期,格式输入支持:'year','week','day','hour'等

示例:date_trunc('week', [下单时间]),返回该行"下单时间"字段对应的当周第一天

img

测试结果:在spark_sql中用date_trunc函数可以截取到任意位置(目前仅测试到秒,未测试微秒),然后让后面位置归为0等,且输入可以是date、time_stamp类型,但是输出time_stamp是型;

select date_trunc('year', cast('2021-12-12 09:32:05' as timestamp));
 
输出:2021-01-01 00:00:00
 
select date_trunc('mm', cast('2021-12-12 09:32:05' as timestamp));
 
输出:2021-12-01 00:00:00
 
select date_trunc('dd', cast('2021-12-12 09:32:05' as timestamp));
 
输出:2021-12-12 00:00:00
 
select date_trunc('week', cast('2021-12-12 09:32:05' as timestamp));
 
输出:2021-12-06 00:00:00
 
select date_trunc('hour', cast('2021-12-12 09:32:05' as timestamp));
 
输出:2021-12-12 09:00:00
 
select date_trunc('minute', cast('2021-12-12 09:32:05' as timestamp));
 
输出:2015-03-05 09:32:00
 
select date_trunc('second', cast('2021-12-12 09:32:05' as timestamp));
 
输出:2021-12-12 09:32:05
 
select date_trunc('minute','2021-12-12');
 
输出:2021-12-12 00:00:00
posted @   哩个啷个波  阅读(258)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示