PG extract 函数示例
pg 对时间的处理还是很灵活的, + - * / 都有支持
期间有个extract 函数还是很有用的,我们先来看看几个例子:[code]
postgres=# select extract(epoch from '1970-01-01'::timestamp) ;
date_part
-----------
0
(1 row)
postgres=# select extract(epoch from '1970-01-01 00:00:01'::timestamp) ;
date_part
-----------
1
(1 row)
postgres=# select extract(epoch from now()-'2013-07-01'::timestamp) ;
date_part
----------------
1936767.072764
(1 row)
postgres=#
[/code]上面的例子是求出从1970-01-01 00:00:00 开始的秒数
extract 函数的功能是从时间中抽出相应的字段
他的使用格式:
EXTRACT(field FROM source)
其中field 包含以下几个值:
century: 世纪
postgres=# select extract(century from '2013-07-01'::date) ;
date_part
-----------
21
(1 row)
day : 一个月里的第几天[code]
postgres=# select extract(day from '2013-07-23'::date) ;
date_part
-----------
23
(1 row)
postgres=# select extract(day from '2013-07-23 09:15:23'::timestamp) ;
date_part
-----------
23
(1 row)
postgres=# select extract(day from '2013-07-23 09:15:23'::date) ;
date_part
-----------
23
(1 row)
postgres=# select extract(day from interval '40 days 3 hours' ) ;
date_part
-----------
40
(1 row)
[/code]decade : 10年期 ,第几个10年
postgres=# select extract(decade from '2013-07-23 09:15:23'::date) ;
date_part
-----------
201
(1 row)
dow 一周里的第几天 (sunday =0 saturday=6)
postgres=# select extract(dow from '2013-07-23 09:15:23'::date) ;
date_part
-----------
2
(1 row)
postgres=# select extract(dow from '2013-07-21 09:15:23'::date) ;
date_part
-----------
0
(1 row)
doy : 一年里的第几天(1-365/366)
postgres=# select extract(doy from '2013-07-21 09:15:23'::date) ;
date_part
-----------
202
(1 row)
hour: 一天里小时数(0-23)
postgres=# SELECT EXTRACT(HOUR FROM TIMESTAMP '2013-07-21 09:15:23');
date_part
-----------
9
(1 row)
postgres=# select extract(hour from '2013-07-21 09:15:23'::date) ;
date_part
-----------
0
(1 row)
注意这里,因为我们把'2013-07-21 09:15:23'::date) 转为date 类型是没有小时的,所以返回0 ,上面的timestamp 是有小时的,正确返回
isodow : ISO 标准一周的天数 sunday=7 monday=1
postgres=# select extract(isodow from '2013-07-21 09:15:23'::date) ;
date_part
-----------
7
(1 row)
postgres=# select extract(dow from '2013-07-21 09:15:23'::date) ;
date_part
-----------
0
(1 row)
postgres=# select extract(isodow from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
7
(1 row)
isoyear: ISO 标准的年 : (
ISO标准的纪年是从周一开始,以一月4号之前的周一为新的纪年的开始,跟公元纪年有区别,所以一年的一月份的前几天,或者12月的后几天可能会跟公元纪年法有区别:
postgres=# select extract(isoyear from '2013-01-01'::date) ;
date_part
-----------
2013
(1 row)
postgres=# select extract(isoyear from '2012-12-31'::date) ;
date_part
-----------
2013
(1 row)
postgres=# select extract(isoyear from '2012-12-30'::date) ;
date_part
-----------
2012
(1 row)
postgres=# select extract(dow from '2012-12-31'::date) ;
date_part
-----------
1
(1 row)
microseconds: 微秒
用微秒标识的 秒 的部分,包括后面小数部分:
postgres=# select extract(microseconds from interval '3 days 5 mins 3.5 sec') ;
date_part
-----------
3500000
(1 row)
postgres=# select extract(microseconds from '2013-07-21 09:15:23'::timestamp)
postgres-# ;
date_part
-----------
23000000
(1 row)
millennium : 千禧年 ,千年纪年
目前是21世纪,第3个千禧年
postgres=# select extract(millennium from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
3
(1 row)
minute: 分钟(0-59)
postgres=# select extract(minute from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
15
(1 row)
month : 月份 对timestamp 类型 返回1-12, 对interval 类型返回0-11
postgres=# select extract(month from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
7
(1 row)
postgres=# select extract(month from interval ' 7 months 5 days' ) ;
date_part
-----------
7
(1 row)
postgres=# select extract(month from interval ' 5 days' ) ;
date_part
-----------
0
(1 row)
postgres=# select extract(month from interval ' 12 months 5 days' ) ;
date_part
-----------
0
(1 row)
postgres=# select extract(month from interval ' 11 months 5 days' ) ;
date_part
-----------
11
(1 row)
quarter : 季度
postgres=# select extract(quarter from '2013-07-21 09:15:23'::timestamp) ;
date_part
-----------
3
(1 row)
postgres=# select extract(quarter from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
2
(1 row)
second : 秒 (0-59)
postgres=# select extract(second from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
23
(1 row)
week : 周记
postgres=# select extract(week from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
25
(1 row)
year: 年纪
postgres=# select extract(year from '2013-06-21 09:15:23'::timestamp) ;
date_part
-----------
2013
(1 row)
PG extract 函数示例的更多相关文章
- Format 函数示例
Format 函数示例本示例显示用 Format 函数做格式化输出的不同用法.对于日期分隔号(/),时间分隔号(:),以及 AM/ PM 等文本而言,其真正的显示格式会因计算机上的国际标准不同而有所差 ...
- php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容
extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容 它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具
- ThinkPHP邮件发送函数示例
ThinkPHP邮件发送函数示例详解 /** * 发送邮件 * @param $tomail * @param $subject * @param $body * @param string $con ...
- PHP extract() 函数
PHP extract() 函数从数组中把变量导入到当前的符号表中. 对于数组中的每个元素,键名用于变量名,键值用于变量值. 第二个参数 type 用于指定当某个变量已经存在,而数组中又有同名元素时, ...
- (转)PHP中extract()函数的妙用
近日在看一个牛人的代码时,看到一个非常好用的函数:extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具,比方说,可以很方便的提取$_ ...
- (转)PHP中extract()函数的妙用
近日在看一个牛人的代码时,看到一个非常好用的函数:extract(),它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具,比方说,可以很方便的提取$_ ...
- 数据分析与展示——Matplotlib基础绘图函数示例
Matplotlib库入门 Matplotlib基础绘图函数示例 pyplot基础图表函数概述 函数 说明 plt.plot(x,y,fmt, ...) 绘制一个坐标图 plt.boxplot(dat ...
- MySQL EXTRACT() 函数
定义和用法 EXTRACT() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等. 语法 EXTRACT(unit FROM date) date 参数是合法的日期表达式.unit 参 ...
- python3使用ctypes在windows中访问C和C++动态链接库函数示例
python3使用ctypes在windows中访问C和C++动态链接库函数示例 这是我们的第一个示例,我们尽量简单,不传参,不返回,不访问其他的动态链接库 一 测试环境介绍和准备 测试环境: 操作系 ...
随机推荐
- Flutter实战视频-移动电商-50.持久化_shared_preferences
50.持久化_shared_preferences 当app关掉了.再进去的时候 ,购物车的内容还是存在. sqflite提供这个来操作SQLite数据库 flutter提供三种持久化的工具 今天要学 ...
- UVA - 11987 Almost Union-Find 并查集的删除
Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...
- C#基础:线程之异步回调(委托)
异步回调,什么是异步回调?我是这样理解的,当主线程在执行一段代码的时候,我们用委托执行了一个线程,这个线程要返回一个结果,关键是什么时候返回这个结果,异步回调就是在这个线程执行完成后立即返回这个线程的 ...
- IOS Carthage安装、使用
一.Carthage的安装和使用1.安装homebrew后输入如下命令 $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercont ...
- HDU3555【数位DP】
入门...还在学习中,先贴一发大牛博客 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意: 给一个数字n,范围在1~2^63-1,求1~ ...
- js函数和变量的执行顺序【易错】
js函数和变量的声明与执行顺序 一.函数执行顺序 1.正常顺序 function f(){ alert(2); } f(); //alert 2 所有浏览器都能测试通过. 2.倒序调用 f(); // ...
- 在win下启动memcached
memcached -m 64 -p 11211 -vvv 设置默认内存64,默认端口11211 ,输出功能及警告错误等信息
- 2016 Noip提高组
2557. [NOIP2016]天天爱跑步 ★★☆ 输入文件:runninga.in 输出文件:runninga.out 简单对比时间限制:2 s 内存限制:512 MB [题目描述] ...
- mysql--浅谈查询1
这是对自己学习燕十八老师mysql教程的总结,非常感谢燕十八老师. 依赖软件:mysql5.6 系统环境:win 在谈查询之前,先说一个特别重要的概念 一定将列名看成变量,既然是变量就可以运算 一定将 ...
- echarts相关属性设置(3)环状图
option = { grid: { left: '3%', top: '0%', // height: 500, right: '30%', containLabel: true, }, legen ...