postgresql----时间类型

postgresql支持的时间类型如下图所示:

日期 date:

建议日期的输入格式为1997-01-01,虽然也支持19970101,1/1/1997,Jan-1-1997等多种格式。

时间戳 timestamp[(p)] with(without) time zone:

其实配置文件是可以设置时区的,且做上层业务时也不会在多个时区间切换,所以一般使用无时区的时间戳就可以满足需要了。

建议时间戳的输入格式为1997-01-01 00:00:00

时间 time[(p)] with(without) time zone:

同样无时区的时间也是可以满足需要的,只表示一天的时间点,不包含日期,可以有如下格式:

12:00:00,120000,12:00

8:00 AM,8:00 PM

时间间隔 interval [fields][(p)]

我还是比较喜欢明确的时间间隔表示方法,如:

1 year 2 months 3 days 4 hours 5 minutes 6 seconds

可以用单词复数,也可以不用

test=# select interval ‘1 year 2 months 3 days 4 hours 5 minutes 6 seconds‘;
           interval
-------------------------------
 1 year 2 mons 3 days 04:05:06
(1 row)

test=# select interval ‘2 year 2 months 3 days 4 hours 5 minutes 6 seconds‘;
            interval
--------------------------------
 2 years 2 mons 3 days 04:05:06
(1 row)

test=# select interval ‘2 year 2 month 3 day 4 hour 5 minute 6 second‘;
            interval
--------------------------------
 2 years 2 mons 3 days 04:05:06
(1 row)

单词太复杂?记不住?

test=# select interval ‘1 y 2 mon 2d 12:09:12‘;
           interval
-------------------------------
 1 year 2 mons 2 days 12:09:12
(1 row)

test=# select interval ‘1 y 2 mon 2d 12h 09m 12s‘;
           interval
-------------------------------
 1 year 2 mons 2 days 12:09:12
(1 row)

几个特殊日期和时间

以下是一些时间运算的示例:

test=# select timestamp(1) without time zone ‘2016-07-08 12:00:00.234‘;
timestamp
-----------------------
2016-07-08 12:00:00.2
(1 row)

test=# select time(1) without time zone ‘2016-07-08 12:00:00.234‘;
time
------------
12:00:00.2
(1 row)

test=# select date‘2016-07-08‘ - 7;
  ?column?
------------
 2016-07-01
(1 row)

test=# select date‘2016-07-08‘ + 7;
  ?column?
------------
 2016-07-15
(1 row)

test=#
test=# select date‘2016-07-08‘ + interval‘1 day 2h‘;
      ?column?
---------------------
 2016-07-09 02:00:00
(1 row)

test=# select date‘2016-07-08‘ + time‘22:00‘;
      ?column?
---------------------
 2016-07-08 22:00:00
(1 row)

test=# select interval‘1day‘ + interval‘1h‘;
    ?column?
----------------
 1 day 01:00:00
(1 row)

test=# select timestamp‘2016-07-08 22:00:00‘ + interval‘2hour‘;
      ?column?
---------------------
 2016-07-09 00:00:00
(1 row)

test=# select timestamp‘2016-07-08 22:00:00‘ - date‘2016-07-08‘;
 ?column?
----------
 22:00:00
(1 row)

test=# select 10*interval‘1h‘;
 ?column?
----------
 10:00:00
(1 row)

时间函数:

函数 返回类型 描述
示例

结果
age(timestamptimestamp) interval 计算两个时间戳的时间间隔
select age(timestamp ‘2001-04-10‘,

timestamp ‘1957-06-13‘);

43 years 9 mons 27 days
age(timestamp) interval 计算current_date与入参时间戳的时间间隔
select age(timestamp

‘2016-07-07 12:00:00‘);

12:00:00
clock_timestamp() timestamp with time zone 当前时间戳(语句执行时变化) select clock_timestamp(); 2016-07-08 15:14:04.197732-07
current_date date 当前日期 select current_date; 2016-07-08
current_time time with time zone 当前时间 select current_time; 15:15:56.394651-07
current_timestamp timestamp with time zone 当前时间戳 select current_timestamp; 2016-07-08 15:16:50.485864-07
date_part(texttimestamp) double precision 获取时间戳中的某个子域,其中text可以为year,month,day,hour,minute,second等
select

date_part(‘year‘,timestamp‘2016-07-08 12:05:06‘),

date_part(‘month‘,timestamp‘2016-07-08 12:05:06‘),

date_part(‘day‘,timestamp‘2016-07-08 12:05:06‘),
date_part(‘hour‘,timestamp‘2016-07-08 12:05:06‘),

date_part(‘minute‘,timestamp‘2016-07-08 12:05:06‘),

date_part(‘second‘,timestamp‘2016-07-08 12:05:06‘);


2016 | 7 | 8 | 12 | 5 | 6

date_part(textinterval) double precision 功能同上,只是第二个入参为时间间隔 select date_part(‘hour‘,interval‘1 day 13:00:12‘); 13
date_trunc(texttimestamp) timestamp
将时间戳截断成指定的精度,

指定精度后面的子域用0补充


select date_trunc(‘hour‘,

timestamp‘2016-07-08 22:30:33‘);

2016-07-08 22:00:00
date_trunc(textinterval) interval 功能同上,只是第二个入参为时间间隔 select date_trunc(‘hour‘,interval‘1 year 2 mon 3 day 22:30:33‘); 1 year 2 mons 3 days 22:00:00
extract(field from timestamp) double precision 功能同date_part(texttimestamp) select extract(hour from timestamp‘2016-07-08 22:30:29‘); 22
extract(field from interval) double precision 功能同date_part(textinterval) select extract(hour from interval‘1 day 13:00:12‘); 13
isfinite(date) boolean 测试是否为有穷日期 select isfinite(date‘2016-07-08‘),isfinite(date‘infinity‘); t,f
isfinite(timestamp) boolean 测试是否为有穷时间戳 select isfinite(timestamp‘2016-07-08‘); t
isfinite(interval) boolean 测试是否为有穷时间间隔 select isfinite(interval‘1day 23:02:12‘); t
justify_days(interval) interval 按照每月30天调整时间间隔 select justify_days(interval‘1year 45days 23:00:00‘); 1 year 1 mon 15 days 23:00:00
justify_hours(interval) interval 按照每天24小时调整时间间隔 select justify_hours(interval‘1year 45days 343hour‘); 1 year 59 days 07:00:00
justify_interval(interval) interval 同时使用justify_days(interval)和justify_hours(interval) select justify_interval(interval‘1year 45days 343hour‘); 1 year 1 mon 29 days 07:00:00
localtime time 当日时间 select localtime; 15:45:18.892224
localtimestamp timestamp 当日日期和时间 select localtimestamp; 2016-07-08 15:46:55.181583
make_date(year intmonth intday int) date 创建一个日期 select make_date(2016,7,8); 2016-07-08

make_interval(

years int DEFAULT 0,

months int DEFAULT 0,

weeks int DEFAULT 0,

days int DEFAULT 0,

hours int DEFAULT 0,

mins int DEFAULT 0,

secs double precision

DEFAULT 0.0)

interval 创建一个时间间隔 select make_interval(1,hours=>3); 1 year 03:00:00

make_time(

hour int,

min int,

sec double precision)

time 创建一个时间 select make_time(9,21,23); 09:21:23

make_timestamp(

year intmonth int,

day inthour int,

min int,

sec double precision)

timestamp 创建一个时间戳 select make_timestamp(2016,7,8,22,55,23.5); 2016-07-08 22:55:23.5

make_timestamptz(year int,

month int,

day inthour int,

min intsec double precision, [ timezone text ])

timestamp with time zone 创建一个带有时区的时间戳 select make_timestamptz(2016,7,8,22,55,23.5); 2016-07-08 22:55:23.5-07
now() timestamp with time zone 当前日期和时间 select now(); 2016-07-08 15:55:30.873537-07
statement_timestamp() timestamp with time zone 同now()  select statement_timestamp(); 2016-07-08 15:56:07.259956-07
timeofday() text
当前日期和时间,包含周几,

功能与clock_timestamp()类似

select timeofday(); Fri Jul 08 15:57:51.277239 2016 PDT
transaction_timestamp() timestamp with time zone 事务开始时的时间戳 select transaction_timestamp(); 2016-07-08 16:01:25.007153-07
to_timestamp(double precision) timestamp with time zone
Convert Unix epoch

(seconds since 1970-01-01

00:00:00+00) to timestamp

select to_timestamp(1284352323);
2010-09-12 21:32:03-07

pg_sleep(seconds double precision);  
当前会话休眠seconds秒

select pg_sleep(5);  
pg_sleep_for(interval)   当前会话休眠多长时间的间隔 select pg_sleep_for(‘5 seconds‘);  
pg_sleep_until(timestamp with time zone)   当前会话休眠至什么时间点 select pg_sleep_until(‘2016-07-08 23:59:59‘);  
时间: 2024-08-10 17:18:30

postgresql----时间类型的相关文章

SQL Date 时间类型处理

SQL 日期(Dates)   2019-10-17 22:17:26 当我们处理日期时,最难的任务恐怕是确保插入的日期的格式,与数据库中日期列的格式相匹配. 保存的如果是日期部分,查询不会有太大问题.但是如果涉及到时间部分,情况就有点复杂了. 下面我们先看看内建日期处理函数 SQL Server Date函数 下面列举出了SQL Server中最重要的内建日期函数: 1.GETDATE()  返回当前日期和时间      语法: GETDATE() 下面是SELECT语句: SELECT GE

跟王老师学MySQL:MySQL数据类型之日期与时间类型

跟王老师学MySQL:MySQL数据类型之日期与时间类型 主讲教师:王少华   QQ群号:483773664 学习内容 YEAR类型的特点及使用 TIME类型的特点及使用 DATE类型的特点及使用 DATETIME类型的特点及使用 TIMESTAMP类型的特点及使用 MySQL表示日期和时间的数据类型有以下几种: 1.YEAR类型 2.TIME类型 3.DATE类型 4.DATETIME类型 5.TIMESTAMP类型 一.Year类型 (一)简介 year使用一个字符串来表示年份 MySQL中

20160421字符串类型;日期时间类型数学类型

第13天 2016-04-21 一.字符串类型   string Trim() - 去头尾的空格,中间的空格不能去. ToUpper() - 把字符串的字母全变成大写. ToLower() - 把字符串的字母全变成小写. StartsWith("子串") - 是否以“子串”开头,是-true EndsWith("子串") - 是否以“子串”结尾,是-true Contains("子串") - 是否包含“子串”,是-true Substring(起

MongoDB时间类型

mongdb时间类型 Date() 显示当前的时间 new Date 构建一个格林尼治时间   可以看到正好和Date()相差8小时,我们是+8时区,也就是时差相差8,所以+8小时就是系统当前时间 ISODate() 也是格林尼治时间 > Date() Sun Jan 24 2016 20:39:57 GMT+0800 (CST) > new Date() ISODate("2016-01-24T12:40:24.035Z") > ISODate() ISODate(

【转】kettle 的内存设置及输出日志的时间类型

本文转载自:http://blog.csdn.net/dqswuyundong/archive/2010/10/19/5952004.aspx 设置kettle的内存 REM ******************************************************************REM ** Set java runtime options                                     **REM ** Change 256m to high

MySQL中日期和时间类型

1 日期类型 MySql中关于日期的类型有Date/Datetime/Timestamp三种类型. 日期赋值时,允许"不严格"语法:任何标点符都可以用做日期部分或时间部分之间的间割符.例如,'98-12-31 11:30:45'.'98.12.31 11+30+45'.'98/12/31 11*30*45'和'[email protected]@31 11^30^45'是等价的,对于不合法的将会转换为:0000-00-00 00:00:00 1.1 Date格式 此类型的字段,存储数据

Mysql 建表时,日期时间类型选择

mysql(5.5)所支持的日期时间类型有:DATETIME. TIMESTAMP.DATE.TIME.YEAR. 几种类型比较如下: 日期时间类型 占用空间 日期格式 最小值 最大值 零值表示  DATETIME  8 bytes  YYYY-MM-DD HH:MM:SS  1000-01-01 00:00:00 9999-12-31 23:59:59 0000-00-00 00:00:00  TIMESTAMP  4 bytes  YYYY-MM-DD HH:MM:SS  197001010

MySQL:MySQL日期数据类型、MySQL时间类型使用总结

MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型        存储空间      日期格式                日期范围------------  ---------  --------------------- -----------------------------------------datetime      8 bytes  YYYY-MM-DD HH:MM:SS  1000-01-01 00:00:00 ~ 9999-12-31 23:59:5

SQL Server 2008对日期时间类型的改进

微软在备受多年的争议后,终于对日期时间数据类型开刀了,在新版的SQL Server 2008中一口气增加了4种新的日期时间数据类型,包括: Date:一个纯的日期数据类型. Time:一个纯的时间数据类型. DateTime2:新的日期时间类型,将精度提到到了100纳秒. DateTimeOffset:新的日期时间类型,在DateTime2的基础上增加了时区部分. 下面是在SQL Server 2008中日期时间数据类型的一个简单汇总表: 数据类型 格式 取值范围 精度 存储尺寸 date yy

数据库里面DataTime时间类型字段,如果为null时

tran.TransactionTime = bet.CreationDate.ToString() == "0001/1/1 0:00:00" ? DateTime.Now : bet.CreationDate; 数据库里面DataTime时间类型字段,如果为null时,查询出来时间值为"0001/1/1 0:00:00" 这是一个溢出SqlDateTime超出范围的时间值,将它再插入数据库时会报错的 数据库里面DataTime时间类型字段,如果为null时,布布