一、string类型,年月日部分包含的时间统一格式:
| 原数据格式(时间字段为string类型) |
取数时间和格式的语法 |
| 2018-11-01 00:12:49.0 |
substr(regexp_replace(created_at,'-',''),0,8)>='20181101' |
| month=201809,day=01 |
concat(month,day)>= '20180901' |
| dt=181101 |
concat('20',a.dt)>=‘20181101’ |
二、日期函数(时间戳)以及各种格式的时间截取,转换方法
1.from_unixtime(bigint unixtime[, string format]):将是将戳转化为日期
将时间的秒值转换成format格式(format可为“yyyy-MM-dd hh:mm:ss”,“yyyy-MM-dd hh”,“yyyy-MM-dd hh:mm”等等)如from_unixtime(1250111000,"yyyy-MM-dd") 得到2009-03-12
(1)时间戳为13位的情况:
hive中from_unixtime可以将一个时间戳转为时间格式,如:
hive> select from_unixtime(1445391280,'yyyy-MM-dd HH:mm:ss');
2015-10-21 09:34:40
问题:
其中第一个参数为bigint型数据,一般是10位的,遇到13位的时间戳,需要去掉最后三位才行,但是bigint型数据不支持直接算数运算,也不支持字符串截取
如,13位时间戳直接转换
hive> select from_unixtime(1445391280000,'yyyy-MM-dd HH:mm:ss');
47772-08-17 01:46:40
两种方法处理此问题:
a.一种是将bigint型数据先转成double型计算之后再转成bigint型,
hive> select from_unixtime(cast(cast(1445391280000 as double)/1000 as bigint),'yyyy-MM-dd HH:mm:ss');
2015-10-21 09:34:40
b.另一种是将bigint型数据转成string型,截取之后再转回bigint型。
hive> select from_unixtime(cast(substr(cast(1445391280 as string),1,10) as bigint),'yyyy-MM-dd HH:mm:ss');
2015-10-21 09:34:40
(2) 案例:时间戳为13位的情况
%jdbc(hive)
select from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy-mm-dd') as dt1,
from_unixtime(cast(substr(pc.ttl,0,10) as int),'yy-MM-dd HH:mm:ss') as dt2,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yyyy-mm-dd') as dt3,
from_unixtime(cast(substr(pc.ttl,0,10) as int),'yyyy-MM-dd HH:mm:ss') as dt4
from xxxx pc
(3)yy-MM-dd和yyMMdd时分秒的划取方法(注意本表中的ttl为string类型)
a.yyMMdd:
%jdbc(hive)
select from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yyMMdd HH:mm:ss') as dt1,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yyMMdd HH:mm') as dt2,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yyMMdd HH') as dt3,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yyMMdd') as dt4,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yyMM') as dt3,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy') as dt4
from xxxx pc
b.yy-MM-dd:
%jdbc(hive)
select from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy-MM-dd HH:mm:ss') as dt1,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy-MM-dd HH:mm') as dt2,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy-MM-dd HH') as dt3,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy-MM-dd') as dt4,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy-MM') as dt5,
from_unixtime(cast(cast(pc.ttl as bigint) / 1000 as bigint), 'yy') as dt6
from xxxx pc
2.unix_timestamp获取当前UNIX时间戳函数:(将日期转化为时间戳)
(1) unix_timestamp()
返回值: bigint
说明: 获得当前时区的UNIX时间戳
(2) unix_timestamp(string date)
返回值: bigint
说明: 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0。
(3)unix_timestamp(string date, string pattern)
返回值: bigint
说明: 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0。
(4)案例如下(yy-MM-dd和yyMMdd两种时间格式):最好使用unix_timestamp(string date, string pattern)转化,表明时间格式
a.yyMMdd
%jdbc(hive)
select a.dt as time,unix_timestamp(a.dt) as time1,
unix_timestamp(a.dt,'yyMMdd') as time2,
concat('20',a.dt) as dt0,unix_timestamp(concat('20',a.dt)) as dt1,
unix_timestamp(concat('20',a.dt),'yyyyMMdd') as dt2
from track.click a
where concat('20',a.dt)>='20181101' and concat('20',a.dt)<='20181103'
limit 100
b.yyyy-MM-dd
%jdbc(hive)
select created_at,
unix_timestamp(created_at) created_at1,
substr(created_at,1,10)as dt0,
unix_timestamp(substr(created_at,1,10))dt1,
unix_timestamp(substr(created_at,1,10),'yyyy-MM-dd') dt2
from trial_sdk.device
where created_at>='2018-11-01' and created_at<='2018-11-03'
limit 100
3.yymmdd和yy-mm-dd日期的切换
方法1: from_unixtime+ unix_timestamp
a.20171205转成2017-12-05
select from_unixtime(unix_timestamp('20171205','yyyymmdd'),'yyyy-mm-dd') from dual;
b.2017-12-05转成20171205
select from_unixtime(unix_timestamp('2017-12-05','yyyy-mm-dd'),'yyyymmdd') from dual;
如:from_unixtime(unix_timestamp(ts),'yyMMdd')
其中ts类型为timestamp,2018-10-11 04:05:29.028
方法2: substr + concat
a.20171205转成2017-12-05
select concat(substr('20171205',1,4),'-',substr('20171205',5,2),'-',substr('20171205',7,2))
from dual;
b.2017-12-05转成20171205
select concat(substr('2017-12-05',1,4),substr('2017-12-05',6,2),substr('2017-12-05',9,2))
from dual;

- python获取hive表时间格式最大分区
#获取表的最大分区 import boto3 from datetime import datetime,timedelta def get_max_partition(db_name,table_n ...
- Hive presto和hive时间格式转换
1.北京时间格式 to unix时间格式 数据格式: 2017-11-17 08:28:13 2017-11-17 08:28:10 2017-11-17 08:27:51.343 2017- ...
- 【,NetCore】WebApi使用统一时间格式
1.在Startup中配置统一时间格式 services.AddMvc() .AddJsonOptions(options => { //配置时间序列化格式 options.Serializer ...
- JS中常用的几种时间格式处理-【笔记整理】
//此处整理点平时常用到的时间格式处理方法 ------------------------------------------- //时间格式化函数 Date.prototype.format = ...
- Hive 笔记
DESCRIBE EXTENDED mydb.employees DESCRIBE EXTENDED mydb.employees DESCRIBE EXTENDED mydb.employees ...
- Newtonsoft.Json 序列化和反序列化 时间格式
From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...
- Newtonsoft.Json 序列化和反序列化 时间格式 [转]
1.JSON序列化 string JsonStr= JsonConvert.SerializeObject(Entity); eg: A a=new A(); a.Name="Elain ...
- MySQL时间戳和时间格式转换函数
MySQL时间戳和时间格式转换函数:unix_timestamp and from_unixtime unix_timestamp将时间转化成时间戳格式.from_unixtime将时间戳转化成时间格 ...
- 将与系统时间格式不同的字符串格式化为DATETIME类型
若系统时间格式为2012/03/05 08:12:12,那么若将("2012-03-05 08:12:12")格式化为时间变量时会报错,在转化之前先将系统时间格式改变再转换就不会报 ...
随机推荐
- 依赖倒置原则(DIP)
什么是依赖倒置呢?简单地讲就是将依赖关系倒置为依赖接口,具体概念如下: 1.上层模块不应该依赖于下层模块,它们共同依赖于一个抽象(父类不能依赖子类,它们都要依赖于抽象类) 2.抽象不能依赖于具体,具体 ...
- mysql列反转Pivoting
Pivoting是一项可以把行旋转为列的技术.在执行Pivoting的过程中可能会使用到聚合.Pivoting技术应用非常广泛.下面讨论的都是静态的Pivoting查询,即用户需要提前知道旋转的属性和 ...
- Jenkins入门之导航操作
通过前面章节讲解我们已经创建了构建任务,我们已经进入了三层目录,如何回到上一级界面?如何直接回到主界面?如何知道我当前所在的位置? 我们看一下红框框选的部分,为Jenkins导航树,从这个导航树,很多 ...
- Go标准库:Go template用法详解
本文只介绍template的语法和用法,关于template包的函数.方法.template的结构和原理,见:深入剖析Go template. 入门示例 以下为test.html文件的内容,里面使用了 ...
- Scrapy爬虫(4)爬取豆瓣电影Top250图片
在用Python的urllib和BeautifulSoup写过了很多爬虫之后,本人决定尝试著名的Python爬虫框架--Scrapy. 本次分享将详细讲述如何利用Scrapy来下载豆瓣电影To ...
- 关于VS Code使用注意
1]:初次使用vs code或多或少有些问题.比如不小心把最左边的这四个快捷按钮消失.,直接按 alt+v 选择[显示活动板]就行了 2]:修改界面语言 快捷键ctrl+shift+p [修 ...
- 创作型---原型模式(C# ICloneable接口的实现)
在软件系统中,当创建一个类的实例的过程很昂贵或很复杂,并且我们需要创建多个这样类的实例时,可以通过对原来对象拷贝一份来完成创建,这样在内存中不需要创建多个相同的类实例,从而减少内存的消耗和达到类实例的 ...
- Linux-bg和fg命令(19)
使用ctrl+z将程序挂在后台: jobs 查看后台的命令: fg(fore go) 将后台的命令,放置前台(fore)继续执行,比如:fg 2 //等价于vi 2.txt bg(back g ...
- Java学习笔记之——枚举类
枚举可以限定类的值只是有限个,例如:星期,只有星期一到星期天 语法案例:
- C#设计模式之十五迭代器模式(Iterator Pattern)【行为型】
一.引言 今天我们开始讲“行为型”设计模式的第三个模式,该模式是[迭代器模式],英文名称是:Iterator Pattern.还是老套路,先从名字上来看看.“迭代器模式”我第一次看到这个名称,我的理解 ...