参考文章: impala 时间日期函数全解

impala 下的SQL时间函数

--======================================================================
---------------------------------- use ---------------------------------
--======================================================================SELECT datadate,session_infoFROM databaseWHERE datadate >= NOW() - INTERVAL 5 DAYORDER BY datadate DESC;
--…………………………………………………………………………………………    SELECT datadate,session_infoFROM databaseWHERE datadate >= from_unixtime(unix_timestamp(now() - interval 5 days),'yyyyMMdd')GROUP BY datadateORDER BY datadate DESC;
--…………………………………………………………………………………………    SELECT from_unixtime(unix_timestamp(create_date),'yyyy-MM-dd HH:mm:ss') FROM health_assess_data
--======================================================================
---------------------------- hive 日期函数 -----------------------------
--======================================================================
--格式化日期from_unixtime(bigint unixtime[, string format])from_unixtime(int, 'yyyy/MM/dd HH:mm')--注意参数Return type: string-- 将指定的时间戳,格式化为字符串. 时间戳参数应该是秒数格式, -- 所以该参数需要用 unix_timestamp() 包一下.-- 注意月份和分钟对应的格式字符串, 常用的格式有:"yyyy-MM-dd HH:mm:ss.SSSSSS", "yyyy-MM-dd HH:mm:ss", "dd/MM/yyyy HH:mm:ss.SSSSSS", "MMM dd, yyyy HH.mm.ss (SSSSSS)"usage:from_unixtime(1392394861,"yyyy-MM-dd");select from_unixtime(1392394861,"yyyy-MM-dd");--把秒数转成时间戳select from_unixtime(cast(cast(1000.0 as decimal) as bigint));SELECT from_unixtime(unix_timestamp(create_date),'yyyy-MM-dd HH:mm:ss') FROM health_assess_data--……………………………………………………………………………………………………
-- 时间戳取整--Impala 2.11 之前的取整当前时间的写法:select trunc(now(), 'YEAR') --取整到年份, 得到当年 1 月 1 日 0 点 0 分select trunc(now(), 'MONTH') --取整到月份, 得到当月 1 日 0 点 0 分select trunc(now(), 'DD') --取整到日期, 得到当天 0 点 0 分select trunc(now(), 'DAY') --取整到星期, 得到本星期第一天的 0 点 0 分select trunc(now(), 'HH24') --取整到小时, 得到当前小时的 0 分select trunc(now(), 'MI') --取整到分钟, 得到当前分钟 0 秒--Impala 2.11 之后增加了 date_trunc() 函数, 下面是几个取整的写法:date_trunc('year',now())date_trunc('month',now())date_trunc('week',now())date_trunc('day',now())date_trunc('hour',now())date_trunc('minute',now())--date_trunc() 的语法和 date_part() 类似, 下面是完整的时间 part 列表:microsecondsmillisecondssecondminutehourdayweekmonthyeardecadecenturymillennium--……………………………………………………………………………………………………
-- 时间加减时间戳可以直接加减 interval n days/months/years/hours/minutes .也可以使用下面的函数:years_add(timestamp t, int n)years_sub(timestamp t, int n)months_add(timestamp t, int n)months_sub(timestamp t, int n)days_add(timestamp t, int n)days_sub(timestamp t, int n)hours_add(timestamp t, int n)hours_sub(timestamp t, int n)minutes_add(timestamp t, int n)minutes_sub(timestamp t, int n)--也可以用下面两个通用的函数:date_add(timestamp startdate, int days)date_add(timestamp startdate, interval_expression)date_sub(timestamp startdate, int days)date_sub(timestamp startdate, interval_expression)--……………………………………………………………………………………………………
-- 时间戳提取date_part('year', now())extract(now(), 'year')extract(year from now())--……………………………………………………………………………………………………
--把时间转化成时间戳select cast('1966-07-30' as timestamp);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);--……………………………………………………………………………………………………
--增加月份add_months(timestamp date, int months)add_months(timestamp date, bigint months)Return type: timestampusage: --增加月份add_months(now(),1)select now(), add_months(now(), 2);select now(), add_months(now(), -1);--……………………………………………………………………………………………………
--增加日期adddate(timestamp startdate, int days), adddate(timestamp startdate, bigint days)Return type: timestampusage:adddate(now(),1)--……………………………………………………………………………………………………
--当前时间戳current_timestamp()和now()等价--……………………………………………………………………………………………………
--日期相减datediff(string enddate, string startdate)Return type: intusage:datediff("2018-08-05", "2018-08-03")--相差的天数select now() as right_now, datediff(now() + interval 5 days,now()) as in_5_years;select datediff('2019-11-10','2019-11-20');-- 两个时间戳比较datediff(timestamp enddate, timestamp startdate) ,相差多少天, 精度是天timestamp_cmp(now() + interval 70 minutes, now()), 比较两个时间戳的大小, 本例的结果为 1impala 没有好用的 timestamp_diff() 函数, 比如我们想要知道两个时间相差多少个小时, 不能直接求出, 下面是一个简单的步骤:1. 先算出一个小时对应的秒数是多少2. 将两个时间都转成秒数, 然后做差, 然后除以一个小时的秒数.
--……………………………………………………………………………………………………
--得到天,得到月份day(string date)Return type: intusage: day("2018-08-05")--取天数select now(), day(now());--……………………………………………………………………………………………………
--得到星期英文dayname(string date) Return type: stringusage:dayname("2018-08-05") Sunday--英文下的星期几select dayname('2004-06-13');--……………………………………………………………………………………………………
--得到这一天是这周的第几天dayofweek(string date)  1 (Sunday) to 7 (Saturday).Return type: intusage:dayofweek("2018-08-06")--一周的第一天,英文下的星期几select now() as right_now, dayofweek(now()) as todays_day_of_week,dayname(now()) as todays_day_name;--一周的第几天select dayofweek('2004-06-13');--……………………………………………………………………………………………………
--加天数days_add(timestamp startdate, int days) Return type: timestampusage:days_add(now(),2)--……………………………………………………………………………………………………
--减天数days_sub(timestamp startdate, int days)Return type: timestampusage:days_sub(now(), 2)--……………………………………………………………………………………………………
--得到小时hour(string date)Return type: intusage:hour("2018-08-06 12:32:54")--取月份 无效月份为nullselect hour('1970-01-01 15:30:00'),hour('1970-01-01 27:30:00');--……………………………………………………………………………………………………
--增加小时hours_add(timestamp date, int hours)Return type: timestampusage:hours_add(now(),2)--……………………………………………………………………………………………………
--减少hours_sub(timestamp date, int hours)Return type: timestampusage:hours_sub(now(),2)--……………………………………………………………………………………………………
--得到分钟minute(string date)Return type: intusage:minute(now())--……………………………………………………………………………………………………
--增加分钟minutes_add(timestamp date, int minutes)Return type: timestampusage:minutes_add(now(),2)--……………………………………………………………………………………………………
--减少分钟minutes_sub(timestamp date, int minutes)Return type: timestampusage:minutes_sub(now(),2)--……………………………………………………………………………………………………
--加三个月select date_add(cast('2016-01-31' as timestamp), interval 3 months) as 'april_31st';--……………………………………………………………………………………………………
--加三周select now() as right_now, date_add(now(), interval 3 weeks) as in_3_weeks;--……………………………………………………………………………………………………
--加6小时select now() as right_now, date_add(now(), interval 6 hours) as in_6_hours;--……………………………………………………………………………………………………
--得到月份month(string date)Return type: intusage:month("2018-08-06 12:32:54")--……………………………………………………………………………………………………
--上一个月select date_sub(cast('2016-05-31' as timestamp), interval 1 months) as 'april_31st';--……………………………………………………………………………………………………
--6个小时前select now() as right_now, date_sub(now(), interval 6 hours) as 6_hours_ago;--……………………………………………………………………………………………………
--前3周的那一天select now() as right_now,date_sub(now(), interval 3 weeks) as 3_weeks_ago;--……………………………………………………………………………………………………
--距现在之后第7天日期select now() as right_now,date_sub(now(), -7) as last_week;--……………………………………………………………………………………………………
--距现在之前的第七天select now() as right_now,date_sub(now(), 7) as last_week;--……………………………………………………………………………………………………
--截取小时select date_part('hour',now()) as hour_of_day;--……………………………………………………………………………………………………
--截取年份select date_part('year',now()) as current_year;--……………………………………………………………………………………………………
--月份相加months_add(timestamp date, int months)Return type: timestampusage:months_add(now(),3)--……………………………………………………………………………………………………
--减月份months_sub(timestamp date, int months)Return type: timestampmonths_sub(now(),3)--……………………………………………………………………………………………………
--相差月份select months_between('2015-02-28','2015-01-28');last_day(timestamp t)months_between(timestamp newer, timestamp older)
--……………………………………………………………………………………………………
--截取年和月份select now() as right_now, extract(year from now()) as this_year, extract(month from now()) as this_month;--……………………………………………………………………………………………………
--得到秒second(string date)Return type: int--……………………………………………………………………………………………………
--秒加seconds_add(timestamp date, int seconds)Return type: timestamp--……………………………………………………………………………………………………
--秒减seconds_sub(timestamp date, int seconds)Return type: timestamp--……………………………………………………………………………………………………
-- 将秒数转换成时间戳to_timestamp(bigint unixtime)--……………………………………………………………………………………………………
-- 将字符串转换成时间戳to_timestamp(string date, string pattern)--说明: impala 没有直接将时间戳转换为字符串的函数, 所以经常的写法是: from_unixtime(unix_timestamp( t1 ),'yyyyMMdd HH:mm')--……………………………………………………………………………………………………
-- 将时间戳转换为日期字符串to_date(now())--将指定时间戳转换为日期字符串, 日期格式为 yyyy--MM-dd to_date(timestamp) --……………………………………………………………………………………………………
--当前时间戳相对于 linux epoch 的秒数, 不带参数, 返回 '1970-01-01 00:00:00' UTC 到现在的秒数unix_timestamp() unix_timestamp(string datetime),unix_timestamp(string datetime, string format),unix_timestamp(timestamp datetime)Return type: bigint--把时间戳转换成秒数select  unix_timestamp(now())-- 转换到相对于 linux epoch 的秒数unix_timestamp(now()+ interval 3 days), 如果传入 timestamp 参数, 返回该时间戳相对于 linux epoch 的秒数unix_timestamp(string datetime, string format), 还支持传入时间字符串, 返回值还是相对于 linux epoch 的秒数--……………………………………………………………………………………………………
--得到这周是这年的多少周weekofyear(string date)Return type: intusage:weekofyear("2018-08-06 12:32:54")--一年中的第几周select now() as right_now, weekofyear(now()) as this_week;--……………………………………………………………………………………………………
--周加weeks_add(timestamp date, int weeks)Return type: timestampusage:weeks_add("2018-08-06 12:32:54", 1)
--……………………………………………………………………………………………………
--两周之后的季初时间select now() + interval 2 weeks as 2_weeks_from_now, trunc(now() + interval 2 weeks, 'Q') as still_current_quarter;--……………………………………………………………………………………………………
--查询当前时间的季初日期select now() as right_now, trunc(now(), 'Q') as current_quarter;--……………………………………………………………………………………………………
--周减weeks_sub(timestamp date, int weeks)Return type: timestampusage:weeks_sub("2018-08-06 12:32:54", 1)--之前的两周时间点select now() as right_now,weeks_sub(now(), 2) as week_before_last;--……………………………………………………………………………………………………
--得到年year(string date)Return type: int--截取年份select now() as right_now,year(now()) as this_year;--……………………………………………………………………………………………………
--年加years_add(timestamp date, int years)Return type: timestamp--增加一年select now() as right_now, years_add(now(), 1) as next_year;--……………………………………………………………………………………………………
--年减years_sub(timestamp date, int years)Return type: timestamp

impala 时间日期函数全解相关推荐

  1. 时间转化_Excel常见时间日期函数全讲解,10个函数教你如何进行日期转化

    在工作中我们经常会碰到一些需要转化或者计算时间日期的工作,这里就需要我们用到一些常见的Excel时间日期函数.今天我们就通过十个案例来教大家,如何在实际工作中对时间日期进行转化处理. 函数一.显示当前 ...

  2. mysql日期时间操作函数详解

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. win7可以设定每周从哪一天开始,win2003等不能方便的修改.有的是周日开始,有的是周一开始.而 ...

  3. Sql Server函数全解三数据类型转换函数和文本图像函数

    原文:Sql Server函数全解<三>数据类型转换函数和文本图像函数 一:数据类型转换函数 在同时处理不同数据类型的值时,SQL Server一般会自动进行隐士类型转换.对于数据类型相近 ...

  4. Oracle时间日期函数及sql语句

    一.时间日期函数 1.to_date函数 to_date的格式:to_date('要转换的时间','转换成的时间格式'), 两个参数的格式必须匹配,否则会报错. 需要注意的是,在Java中的时间格式是 ...

  5. access日期如何增加年数_ACCESS支持的时间日期函数

    ACCESS支持的时间日期函数 参考文章一: 找了半天,终于在微软的网站找到了,对于做ACCESS数据库开发人来说还是有点用处的,有时间的话我会把它翻译成中文.(注:其中大部分对于Access是适用了 ...

  6. Sybase时间日期函数

    http://yangkun0318.blog.163.com/blog/static/131809433201031295947642/ Sybase时间日期函数 2010-04-12 09:59: ...

  7. R语言-时间日期函数

    R语言时间日期函数 1. 返回当前日期时间,有两种方式: Sys.time() date() 举例 format(Sys.time(), "%a %b %d %X %Y %Z")# ...

  8. PostgreSQL的时间/日期函数使用

    PostgreSQL的常用时间函数使用整理如下: 一.获取系统时间函数 1.1 获取当前完整时间 select now(); david=# select now();now ------------ ...

  9. 收集SQLite中的时间日期函数[ZT]

    声明:文章摘自:http://www.xueit.com/html/2009-02/27_649_00.html 在插入数据时为了插入时间,我自己用了这个方法在数据库中: sql = "IN ...

最新文章

  1. 邁向IT專家成功之路的三十則鐵律 鐵律十四:IT人言談之道-守中
  2. linux修改定时后如何保存文件夹,linux定时任务的一些相关操作汇总
  3. .net core mvc 区域路由设置(配置)
  4. C++设计模式:Template Method
  5. php 7.1.5,Centos 7平滑无缝更新PHP7.1.0到PHP 7.1.5
  6. python 数据分析工具之 numpy pandas matplotlib
  7. 到底什么是IT服务管理
  8. ttribute value is quoted with which must be escaped when used within the value
  9. linux find prune排除某目录或文件
  10. MFC 教程【5_MFC对象的创建】
  11. PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unknow........
  12. 在Mac中如何通过命令对NTFS磁盘格式化
  13. 用java实现学生成绩管理系统(附有详细代码)
  14. 软考试题希赛网爬取过程分享一
  15. 【UmiJS学习】01-快速上手
  16. ss导航java宝典_ss导航绅士宝典app下载-ss导航绅士宝典百度网盘官方版下载v1.1.0-七度网...
  17. java编写singleton程序_java – 在Singleton实现中初始化按需成语与简单静态初始化程序...
  18. python对行为进行推理_一道有意思推理题,用python来解答
  19. 第一章--多媒体技术概述
  20. Redis命令之集合(无序)

热门文章

  1. 解决使用FireFox下Flash上传文件时SESSION丢失的问题(swfupload)
  2. 755 linux,linux系统644、755、777权限详解
  3. Fedora30 工具
  4. 鼠标悬浮显示文字半透明背景
  5. 网络流入门——算法模板,习题和解析
  6. GPD P2MAX用gibMacOS图文安装黑苹果
  7. OSChina 周三乱弹 ——小萝卜变蘑菇大法
  8. 中国吸烟庇护所行业市场供需与战略研究报告
  9. 想做游戏3D建模师,该如何学习,学习哪些方面?
  10. Android UI设计和形成原理(实现三级菜单)