impala日期格式常用转换

获取时间

//获取当前时间
select now(); //时间到毫秒,如:2022-07-21 13:57:14.435929000
select current_timestamp(); //与now()结果一样
select from_unixtime(unix_timestamp(),'yyyy-MM-dd') //如:2022-07-22
select to_date(now()) //如:2022-07-22
select from_unixtime(unix_timestamp()) //如:2022-07-22 09:59:34

时间格式转换

select  from_unixtime(unix_timestamp(),'yyyy/MM/dd HH:mm:ss')  //获取当前时间(格式为:年/月/日 时:分:秒)
select left (cast(now() as string),19)  //2022-07-22 10:01:38
select left (cast(now() as string),16)  //2022-07-22 10:01
select from_unixtime(unix_timestamp(a.字段名),'yyyy') as year,* from 表名 a //转换某个字段的时间格式**时间戳提取**
select date_part('year', now()) //2022
select extract(now(), 'year')//2022
select extract(year from now())//2022select year('2022-07-27') //2022select month('2022-07-27') //7

时间转换为当年1月1日00点

select date_trunc('year',now()) //结果:2022-01-01 00:00:00
select date_trunc('year','2022-07-07 14:35:11') //2022-01-01 00:00:00
select date_trunc('year',a.字段名) as year,* from 表名  //2022-01-01 00:00:00

格式如下:

格式 中文含义 备注
microseconds 微秒 当前时间所在微秒
milliseconds 毫秒 当前时间所在毫秒
second 当前时间所在秒
minute 分钟 当前时间所在分钟
hour 小时 当前时间所在小时
day 当前时间所在天的第1天00点
week 当前时间所在周的第1天00点
month 当前时间所在月份第1天00点
year 当前时间所在年份第1天00点
decade 十年
century 世纪
millennium 一千年

时间差

//获取当前时间半年前的时间select add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-6) //2022-01-28 00:00:00
//当前时间减30天select from_unixtime(unix_timestamp()-60*60*24*30) // 2022-06-28 15:49:55
//当前时间减1天select adddate(from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss'),-1) //2022-07-27 15:54:48

时间的加减

增加天数

 days_add(timestamp startdate,int days)如:select days_add('2022-07-25',1) //2022-07-26 00:00:00select days_add(from_unixtime(unix_timestamp()),2)//2022-07-31 10:17:19select days_add(now(),2)//2022-07-31 10:17:27.241333000

减少天数

 days_sub(timestamp startdate,int days)如:select days_sub(now(),2)  //2022-07-27 10:22:10.202118000select days_sub(from_unixtime(unix_timestamp()),2) //2022-07-27 10:21:35

格式如下:

格式 中文含义
years_add 增加年
years_sub 减少年
months_add 增加月
months_sub 减少月
days_add 增加天
days_sub 减少天
weeks_add 增加周
weeks_sub 减少周
hours_add 增加小时
hours_sub 减少小时
minutes_add 增加分钟
minutes_sub 减少分钟
senconds_add 增加秒
senconds_add 减少秒

注:也可以用公共日期函数

date_add(timestamp startdate, interval_expression)
date_sub(timestamp startdate, int days)
如:select date_add(from_unixtime(unix_timestamp()),2) //2022-07-31 10:57:06select date_sub(from_unixtime(unix_timestamp()),2) //2022-07-27 10:57:13

把时间转化成时间戳

select cast('1966-07-30' as timestamp); //1966-07-30 00:00:00
select cast('1985-09-25 17:45:30.005' as timestamp);
select cast('08:30:00' as timestamp);

把字符串转换成时间戳

cast('2019-10-14 18:00:41' as timestamp)
impala 没有好用的 timestamp_diff() 函数, 比如我们想要知道两个时间相差多少个小时, 不能直接求出, 下面是一个简单的步骤:1. 先算出一个小时对应的秒数是多少2. 将两个时间都转成秒数, 然后做差, 然后除以一个小时的秒数.3. unix_timestamp(finish_time)-unix_timestamp(start_time)可以得出之间的秒数
//计算两个时间间隔小时数
select ( unix_timestamp('2022-07-29 14:40:00')-unix_timestamp('2022-07-29 13:40:00'))/60/60

impala日期格式转换相关推荐

  1. 日期格式转换 java 2016-09-03T00:00:00.000+08:00

    /**   * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  (yyyy-MM-dd'T'HH:mm:ss.SSSZ) TO  yyyy-MM-dd HH:mm:ss   * ...

  2. POJ3751 时间日期格式转换【日期计算】

    时间日期格式转换 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8306   Accepted: 3829 Descript ...

  3. python日期格式转换_python中有关时间日期格式转换问题

    每次遇到pandas的dataframe某列日期格式问题总会哉坑,下面记录一下常用时间日期函数.... 1.字符串转化为日期 str->date import datetime date_str ...

  4. [转载] Java中日期格式转换

    参考链接: Java中的类型转换和示例 Code: /**     * 字符串转换为java.util.Date<br>     * 支持格式为 yyyy.MM.dd G 'at' hh: ...

  5. 1900-01-01t00:00:00+08:00 java_日期格式转换 java 2016-09-03T00:00:00.000+08:00

    标签: /** * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  TO  yyyy-MM-dd HH:mm:ss * @throws ParseException */ pu ...

  6. NUC1421 时间日期格式转换【日期计算】

    时间日期格式转换 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 世界各地有多种格式来表示日期和时间.对于日期的常用格式,在中国常采用格式的是" ...

  7. java 导入excel 日期格式转换

    java 导入excel 日期格式转换 导入日期格式后 获取到的value值为 数值或小数值 直接上硬货 cell = row.getCell(4);String go_time = getCellV ...

  8. js日期格式转换Wed Mar 22 13:38:37 CST 2017 转换 为yyyy-mm-dd

    js日期格式转换:Wed Mar 22 13:38:37 CST 2017 转换 为yyyy-mm-dd <html> <head>test</head> < ...

  9. Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换

    Oracle日期格式转换 to_date,to_char,to_timetamp 相互转换 一.概述 1.本文主要记录Oracle数据库中,常见的日期格式转换.使用 to_char函数,将日期转换为字 ...

  10. 【基础】SimpleDateFormat日期格式转换详解

    SimpleDateFormat是处理日期格式转换的类. 官方API_1.8关于SimpleDateFormat继承于DateFormate截图: SimpleDateFormat的构造器如下: Si ...

最新文章

  1. hbase shell-namespace(命名空间指令)
  2. JS中深浅拷贝 函数封装代码
  3. Leetcode刷题(5)最长公共前缀
  4. python中文注释
  5. 石青建站养站大师 v1.8.6.1
  6. 详解之-js闭包的用途
  7. 3027 - Corporative Network
  8. day25 在继承的背景下属性查找的顺序、组合、多态与接口、鸭子类型
  9. HeadFirstJava——8_接口与抽象类
  10. 1-通用对话系统-阿里大文娱-李亚楠
  11. pyhton根据城市名称获取省份名称
  12. 【有感】聆听哈佛幸福课 (上)
  13. rust拆除建筑_破败建筑除了拆除或修复,变成废墟花园是更浪漫的一种可能
  14. C++ 应用程序无法正常启动0xc000007b
  15. 基于Kinect 2.0深度图像的快速体积测量
  16. C++STL之<set>和<map>
  17. 安装Adobe全家桶时出现The installation cannot continue as the installer file may be damaged.解决方法。
  18. 声声不息,新“声”报到
  19. 【组合数学】指数生成函数 ( 指数生成函数概念 | 排列数指数生成函数 = 组合数普通生成函数 | 指数生成函数示例 )
  20. 读书笔记 - 学会写作: 什么是写作?

热门文章

  1. 如何从官网下载hibernate
  2. csf安装及命令参数
  3. C# BackgroundWorker使用总结
  4. firemonkey mysql_FireMonkey开发技术简明手册.pdf
  5. Android Studio导入安卓源码
  6. 《代码整洁之道》读书笔记
  7. Unity3D面试总结
  8. 基于STM32的智能家居控制系统设计
  9. 用python解决搬砖问题_如何像要饭一般”用“python (一)(非编程类工作/科研搬砖工)...
  10. 永中集成Office的春天能否到来?