在PostgreSQL中,DATE、TIME、TIMESTAMP是三种不同的数据类型。DATE表示日期类型,格式为YYYY-MM-DD或YYYYMMDD;TIME表示时间类型,格式为hh:mi:ss;

TIMESTAM类型的格式一般为'YYYY-MM-DD hh:mi:ss'。

1. current_date、current_time和localtime

current_date按照YYYY-MM-DD格式返回当前日期;current_time获取系统的当前时间;localtime的作用和current_time相同,不同点在于localtime返回的时间不带时区。

test=# select current_date,current_time,localtime;
date | timetz | time
------------+--------------------+-----------------
2017-05-31 | 21:26:47.731091+08 | 21:26:47.731091
(1 row)

2. current_timestamp、localtimestamp和now()

这三个函数的作用都是返回系统当前的日期和时间。

test=# select current_timestamp,localtimestamp,now();
now | timestamp | now
------------------------------+---------------------------+------------------------------
2017-05-31 21:28:24.17628+08 | 2017-05-31 21:28:24.17628 | 2017-05-31 21:28:24.17628+08
(1 row)

3. extract(type from date)

extract函数从日期中提取部分值。

(1)依次提取日期中的年份、月份、日部分

test=# select extract(year from timestamp '2017-05-31 21:31:20') as year,extract(month from timestamp '2017-05-31 21:31:20') as month,extract(day from timestamp '2017-05-31 21:31:20') as day;
year | month | day
------+-------+-----
2017 | 5 | 31
(1 row)

(2)依次查询指定日期是所在年的第几天、所在周的星期几、所在年的第几季度

test=# select extract (doy from timestamp '2017-05-31 21:31:20') as doy,extract (dow from timestamp '2017-05-31 21:31:20') as dow,extract(quarter from timestamp '2017-05-31 21:31:20') as quarter;
doy | dow | quarter
-----+-----+---------
151 | 3 | 2
(1 row)

4. 日期的运算

test=# select date '2017-05-31' + integer '';
?column?
------------
2017-06-10
(1 row) test=# select date '2017-05-31' + interval '5 hour';
?column?
---------------------
2017-05-31 05:00:00
(1 row) test=# select date '2017-05-31' + time '12:00';
?column?
---------------------
2017-05-31 12:00:00
(1 row) test=# select timestamp '2017-05-31 21:31:20' + interval '3 hour';
?column?
---------------------
2017-06-01 00:31:20
(1 row) test=# select date '2017-07-22' - date '2017-05-31';
?column?
----------
52
(1 row) test=# select date '2017-05-31' - integer '';
?column?
------------
2017-05-21
(1 row) test=# select 15 * interval '2 day';
?column?
----------
30 days
(1 row) test=# select 50 * interval '2 second';
?column?
----------
00:01:40
(1 row) test=# select interval '1 hour' / integer '';
?column?
----------
00:30:00
(1 row)

The End!

【PostgreSQL-9.6.3】函数(3)--日期和时间函数的更多相关文章

  1. 20101102--SQL字符串函数 ,日期和时间函数

    --------------------字符串函数------------------------- --ASCII 返回字符串的首字母的ASCII编码 select ASCII('w') selec ...

  2. 日期时间函数 mysql 和sqlserver 中对于常用函数的日期和时间函数的区别

    1. sqlserver中获取时间用getdate(),默认返回格式是2019-01-21 13:58:33.053,具体的年月日,时分秒毫米,年月日之间用短线连接,时分秒之间用冒号连接,秒和毫米之间 ...

  3. MySQL 笔记 —— 日期和时间函数

    [TOC] 获取当前日期的函数和获取当前时间的函数 CURDATE()和CURRENT_DATE()函数获取当前日期:CURTIME()和CURRENT_TIME()函数获取当前时间. mysql&g ...

  4. MySQL日期和时间函数汇总

    本文基于MySQL8.0 本文介绍MySQL关于日期和时间操作的函数. 日期和时间函数 函数 描述 ADDDATE() 给日期值添加时间值 ADDTIME() 添加time CONVERT_TZ() ...

  5. Sql Server函数全解(四)日期和时间函数

      日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外,也可以使用datetime类型的参数,但会忽略这些值的时间部分.相同 ...

  6. MySQL数据库9 - 日期与时间函数

    一 日期和时间函数 函数的概念:按指定格式输入参数,返回正确结果的运算单元 1. 返回当前日期:curdate() current_date() current_date()+0可以将当前日期转换为数 ...

  7. sqlserver常用日期、时间函数和格式

    Sql Server中常用的日期与时间函数1.  当前系统日期.时间    select getdate() 2. dateadd  在向指定日期加上一段时间的基础上,返回新的 datetime 值  ...

  8. Sql Server函数全解<四>日期和时间函数

    原文:Sql Server函数全解<四>日期和时间函数   日期和时间函数主要用来处理日期和时间值,本篇主要介绍各种日期和时间函数的功能和用法,一般的日期函数除了使用date类型的参数外, ...

  9. MySQL数据库—日期与时间函数

    一. 日期和时间函数 函数的概念:按指定格式输入参数,返回正确结果的运算单元 1. 返回当前日期:curdate() current_date() current_date()+0可以将当前日期转换为 ...

  10. web报表工具FineReport常用函数的用法总结(日期和时间函数)

    web报表工具FineReport常用函数的用法总结(日期和时间函数) 说明:凡函数中以日期作为参数因子的,其中日期的形式都必须是yy/mm/dd.而且必须用英文环境下双引号(" " ...

随机推荐

  1. Ubuntu 16.04 安装R和RStudio

    在Ubuntu上安装R和Rstudio的时候碰到了一些依赖项不存在的错误 The following packages have unmet dependencies: r-base-core : D ...

  2. Laravel-Action 对代码的改造

    前言 以往写过俩篇文章[积德篇] 如何少写PHP "烂"代码 https://segmentfault.com/a/11...举枪消灭"烂代码"的实战案例 ht ...

  3. Excel表格

    自己一个一个试出来,并写上解释. 还不熟练,待多写代码多练习. #!/usr/bin/python # -*- coding:utf-8 -*- import os import xlwt impor ...

  4. seminar information (Email template)

      The following is an email example of seminar information   **************** Dear all, It is a plea ...

  5. Linux 实用指令(4)

    目录 实用指令 1.指定运行级别 2.切换到指定运行级别的指令 3.帮助指令 3.1man获得帮助信息 3.2help指令 4.文件目录类 4.1pwd指令 4.2 ls指令 4.3 cd指令 4.4 ...

  6. HDU 3401 Trade

    Trade Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 3401 ...

  7. Codeforces 263C. Appleman and Toastman

    C. Appleman and Toastman time limit per test  2 seconds memory limit per test  256 megabytes input  ...

  8. RestEasy用户指南---第6章.@QueryParam

    转载说明出处:http://blog.csdn.net/nndtdx/article/details/6870391 原文地址 http://docs.jboss.org/resteasy/docs/ ...

  9. 清北学堂模拟赛d7t1 消失的数字

    题目描述 现在,我的手上有 n 个数字,分别是 a1; a2; a3; :::; an.我现在需要删除其中的 k 个数字.当然我不希望随随便便删除,我希望删除 k个数字之后,剩下的 n - k 个数中 ...

  10. 洛谷 P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll

    P2959 [USACO09OCT]悠闲漫步The Leisurely Stroll 题目描述 Bessie looks out the barn door at the beautiful spri ...