PS:http://blog.csdn.net/love_rongrong/article/details/6712883

字符串模糊比较

日期类型的模糊查询是不能直接进行的,要先转换成字符串然后再查询

例子如下:

select * from table where to_char(“timestamp", 'yyyy-mm-dd hh24:mi:ss') like '%08:30:00%'

这里要注意的是postgre的时间处理上,不分大小写,时分秒格式:hh24:mi:ss,这样才能正常的显示

这样查出的就是所有的时间为8点的数据

PostgreSQL的常用时间函数使用整理如下:

一、获取系统时间函数

1.1 获取当前完整时间

select now();

david=# select now();
now
-------------------------------
2013-04-12 15:39:40.399711+08
(1 row) david=#

current_timestamp 同 now() 函数等效。

david=# select current_timestamp;
now
-------------------------------
2013-04-12 15:40:22.398709+08
(1 row) david=#

1.2 获取当前日期

select current_date;

david=# select current_date;
date
------------
2013-04-12
(1 row) david=#

1.3 获取当前时间

select current_time;

david=# select current_time;
timetz
--------------------
15:43:31.101726+08
(1 row) david=#

二、时间的计算

david=# select now();
now
-------------------------------
2013-04-12 15:47:13.244721+08
(1 row) david=#

2.1 两年后

david=# select now() + interval '2 years';
?column?
-------------------------------
2015-04-12 15:49:03.168851+08
(1 row) david=# select now() + interval '2 year';
?column?
-------------------------------
2015-04-12 15:49:12.378727+08
(1 row) david=# select now() + interval '2 y';
?column?
------------------------------
2015-04-12 15:49:25.46986+08
(1 row) david=# select now() + interval '2 Y';
?column?
-------------------------------
2015-04-12 15:49:28.410853+08
(1 row) david=# select now() + interval '2Y';
?column?
-------------------------------
2015-04-12 15:49:31.122831+08
(1 row) david=#

2.2 一个月后

david=# select now() + interval '1 month';
?column?
------------------------------
2013-05-12 15:51:22.24373+08
(1 row) david=# select now() + interval 'one month';
ERROR: invalid input syntax for type interval: "one month"
LINE 1: select now() + interval 'one month';
^
david=#

2.3 三周前

david=# select now() - interval '3 week';
?column?
-------------------------------
2013-03-22 16:00:04.203735+08
(1 row) david=#

2.4 十分钟后

david=# select now() + '10 min';
?column?
-------------------------------
2013-04-12 16:12:47.445744+08
(1 row) david=#

说明:

interval 可以不写,其值可以是:

Abbreviation Meaning
Y Years
M Months (in the date part)
W Weeks
D Days
H Hours
M Minutes (in the time part)
S Seconds

2.5 计算两个时间差

使用 age(timestamp, timestamp)

david=# select age(now(), timestamp '1989-02-05');
age
----------------------------------------
24 years 2 mons 7 days 17:05:49.119848
(1 row) david=#
david=# select age(timestamp '2007-09-15');
age
------------------------
5 years 6 mons 27 days
(1 row) david=#

三、时间字段的截取

在开发过程中,经常要取日期的年,月,日,小时等值,PostgreSQL 提供一个非常便利的EXTRACT函数。

EXTRACT(field FROM source)

field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。

3.1 取年份

david=# select extract(year from now());
date_part
-----------
2013
(1 row) david=#

3.2 取月份

david=# select extract(month from now());
date_part
-----------
4
(1 row) david=#
david=# select extract(day from timestamp '2013-04-13');
date_part
-----------
13
(1 row) david=#
david=# SELECT EXTRACT(DAY FROM INTERVAL '40 days 1 minute');
date_part
-----------
40
(1 row) david=#

3.3 查看今天是一年中的第几天

david=# select extract(doy from now());
date_part
-----------
102
(1 row) david=#

3.4 查看现在距1970-01-01 00:00:00 UTC 的秒数

david=# select extract(epoch from now());
date_part
------------------
1365755907.94474
(1 row) david=#

3.5 把epoch 值转换回时间戳

david=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1369755555 * INTERVAL '1 second';
?column?
------------------------
2013-05-28 23:39:15+08
(1 row) david=#

以上是基本的PG时间/日期函数使用,可满足一般的开发运维应用。

详细用法请参考:

PostgreSQL官方说明:http://www.postgresql.org/docs/9.2/static/functions-datetime.html

分类: Postgresql

[转] PostgreSQL的时间/日期函数使用的更多相关文章

  1. [转帖]PostgreSQL的时间/日期函数使用

    PostgreSQL的时间/日期函数使用 https://www.cnblogs.com/mchina/archive/2013/04/15/3010418.html 这个博客的 文章目录比上一个好十 ...

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

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

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

    http://www.cnblogs.com/mchina/archive/2013/04/15/3010418.html

  4. postgreSQL格式化时间的函数详解

    数据类型格式化函数:    PostgreSQL格式化函数提供一套有效的工具用于把各种数据类型(日期/时间.integer.floating point和numeric)转换成格式化的字符串以及反过来 ...

  5. SQL-数学、字符串、时间日期函数和类型转换

    --数学函数 --ABS绝对值,select ABS(-99)--ceiling取上限,select CEILING(4.5)--floor去下限select FLOOR(4.5)--power 几次 ...

  6. SQLite中的时间日期函数(转)

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...

  7. SQL servcer 时间日期函数、数据类型转换

    1.时间日期函数 2.数据类型转换 3.习题 建立两个表,一个部门表,一个人员表.部门:部门的编号,部门的名称,部门的职责.人员:人员的编号,姓名,年龄,性别,cid所属部门

  8. SQL server 数据库——数学函数、字符串函数、转换函数、时间日期函数

    数学函数.字符串函数.转换函数.时间日期函数 1.数学函数 ceiling()--取上限  select ceiling(oil) as 油耗上限 from car floor()--取下限 sele ...

  9. SQLite中的时间日期函数

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...

随机推荐

  1. How to scroll the window using JQuery $.scrollTo() function

    $('html, body').animate({scrollTop: $("#page").offset().top}, 2000); http://stackoverflow. ...

  2. 『重构--改善既有代码的设计』读书笔记----Inline Temp

    与Inline Method相同,有时候犹豫需要Extract Method,需要对一些临时变量进行内联,而这个往往是Replace Temp with Query的一部分.简单来说,当你看到这种 d ...

  3. @font-face扒站的步骤

    今天模仿百度首页手机版的时候遇到的@font-face的问题,现在整理一下. 问题:图中红色区域,在拷贝F12样式的时候,并没有出现这些小图标.        图1:百度的效果             ...

  4. Delphi笔记(GL_Scene四轴飞行器模型)

    有了前的一篇做铺垫,已经简单的说了GL_Scene的下载安装和一个简单的实例制作.现在就要开始制作一个3D的模型了,具体的步骤就不再这里多说了,直接上图和代码吧! [第一版]先看一下最开始的版本吧,比 ...

  5. LightOJ 1317 第六周比赛A题

    A - A Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description Y ...

  6. KeyPress事件

    在做一个小demo的时候,发现在文本框中输入一个数字,按下“+”,数字增加了,但是“+”仍旧存在的问题,解决方案:提前执行键盘press事件 private void txtNum_KeyPress( ...

  7. xcode5 自定义模板

    经过一番周折,终于在xcode5上实现了一个简单的自定义模板,在项目中集成NSLogger库(增强NSLog的功能,https://github.com/fpillet/NSLogger)——新建项目 ...

  8. android防止系统截屏

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow() ...

  9. Remember the Word

    uvalive3942:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pag ...

  10. 简单的猜数字(JAVA版)

    按书上的样例来操作的. 不过,书上提到的BUG,我没有在看下一章时就解决了哈.. 从网上查找的删除数组元素的方法. 其实,将数据结构更改为ARRAYLIST,可能更简单.:) GameHelper.j ...