postgres的强制类型转换与时间函数

一。类型转换postgres的类型转换:通常::用来做类型转换,timestamp到date用的比较多select  now()::dateselect  now()::varchar

示例1:日期的varchar计算成dateselect ‘2012-11-15 16:15:56.377000+08‘::timestamp::dateselect ‘2012-11-15 16:15:56.377000+08‘::date结果: 2012-11-15

二。时间的类型转换与相对时间

//注意java的timestamp将来在sql中体现的varchar的形式‘2012-11-15 16:15:56.377000+08’,这样的串可以计算时间差。

假如表中的一条记录的publishdate是 ‘2012-11-15 16:15:56.377000+08‘,想确认该记录是不是过去24小时之内publish的记录,可以使用如下的判断:

select  extract(epoch from now() - ‘2012-11-15 16:15:56.377000+08‘)< 24*3600 

select now() - ‘2012-11-15 16:15:56.377000+08‘ < ‘24 hours‘

select now() - ‘2012-11-15 16:15:56.377000+08‘ < ‘1 days‘ or select now() - ‘2012-11-16 16:15:56.377000+08‘ < ‘1 day‘

select now()::date-‘2012-11-15 16:15:56.377000+08‘::date < 1

注:相对时间表示时间范围,通常用于统计,定时任务 。除了相对时间,‘today’使用的也比较多。比如取当天的记录使用:publishdate::date = ‘today‘

三。时间函数Extract用于提取绝对时间的年,月,日.....; 相对时间的秒值。EXTRACT(field FROM source)

The extract function retrieves subfields such as year or hour from date/time values. source must be a value expression of type timestamp, time, or interval. (Expressions of type date will be cast to timestamp and can therefore be used as well.) field is an identifier or string that selects what field to extract from the source value. The extract function returns values of type double precision. The following are valid field names:

century
The century

SELECT EXTRACT(CENTURY FROM TIMESTAMP ‘2000-12-16 12:21:13‘);
Result: 20
SELECT EXTRACT(CENTURY FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 21

The first century starts at 0001-01-01 00:00:00 AD, although they did not know it at the time. This definition applies to all Gregorian calendar countries. There is no century number 0, you go from -1 to 1. PostgreSQL releases before 8.0 did not follow the conventional numbering of centuries, but just returned the year field divided by 100.

day
The day (of the month) field (1--31)

SELECT EXTRACT(DAY FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 16
decade
The year field divided by 10

SELECT EXTRACT(DECADE FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 200
dow
The day of the week (0--6; Sunday is 0) (for timestampvalues only)

SELECT EXTRACT(DOW FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 5

Note that extract‘s day of the week numbering is different from that of the to_char function.

doy
The day of the year (1--365/366) (for timestampvalues only)

SELECT EXTRACT(DOY FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 47
epoch
For date and timestamp values, the number of seconds since 1970-01-01 00:00:00-00 (can be negative); for intervalvalues, the total number of seconds in the interval

SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE
‘2001-02-16 20:38:40-08‘);
Result: 982384720
SELECT EXTRACT(EPOCH FROM INTERVAL ‘5 days 3 hours‘);
Result: 442800

Here is how you can convert an epoch value back to a time stamp:

SELECT TIMESTAMP WITH TIME ZONE ‘epoch‘ + 982384720 *
INTERVAL ‘1 second‘;
hour
The hour field (0--23)

SELECT EXTRACT(HOUR FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 20
microseconds
The seconds field, including fractional parts, multiplied by 1 000 000. Note that this includes full seconds.

SELECT EXTRACT(MICROSECONDS FROM TIME ‘17:12:28.5‘);
Result: 28500000
millennium
The millennium

SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP ‘2001-02-16
20:38:40‘);
Result: 3

Years in the 1900s are in the second millennium. The third millennium starts January 1, 2001. PostgreSQL releases before 8.0 did not follow the conventional numbering of millennia, but just returned the year field divided by 1000.

milliseconds
The seconds field, including fractional parts, multiplied by 1000. Note that this includes full seconds.

SELECT EXTRACT(MILLISECONDS FROM TIME ‘17:12:28.5‘);
Result: 28500
minute
The minutes field (0--59)

SELECT EXTRACT(MINUTE FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 38
month
For timestamp values, the number of the month within the year (1--12) ; for intervalvalues the number of months, modulo 12 (0--11)

SELECT EXTRACT(MONTH FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 2
SELECT EXTRACT(MONTH FROM INTERVAL ‘2 years 3 months‘);
Result: 3
SELECT EXTRACT(MONTH FROM INTERVAL ‘2 years 13 months‘);
Result: 1
quarter
The quarter of the year (1--4) that the day is in (for timestampvalues only)

SELECT EXTRACT(QUARTER FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 1
second
The seconds field, including fractional parts (0 - 59(3))

SELECT EXTRACT(SECOND FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 40
SELECT EXTRACT(SECOND FROM TIME ‘17:12:28.5‘);
Result: 28.5
timezone
The time zone offset from UTC, measured in seconds. Positive values correspond to time zones east of UTC, negative values to zones west of UTC.
timezone_hour
The hour component of the time zone offset
timezone_minute
The minute component of the time zone offset
week
The number of the week of the year that the day is in. By definition (ISO 8601), the first week of a year contains January 4 of that year. (The ISO-8601 week starts on Monday.) In other words, the first Thursday of a year is in week 1 of that year. (for timestamp values only) Because of this, it is possible for early January dates to be part of the 52nd or 53rd week of the previous year. For example, 2005-01-01 is part of the 53rd week of year 2004, and 2006-01-01is part of the 52nd week of year 2005.

SELECT EXTRACT(WEEK FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 7
year
The year field. Keep in mind there is no 0 AD, so subtracting BC years from ADyears should be done with care.

SELECT EXTRACT(YEAR FROM TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 2001

The extract function is primarily intended for computational processing. For formatting date/time values for display, see section 7.8 Data Type Formatting Functions.

The date_part function is modeled on the traditional Ingres equivalent to the SQL-standard function extract:

date_part(‘field‘, source)

Note that here the field parameter needs to be a string value, not a name. The valid field names for date_part are the same as for extract.

SELECT date_part(‘day‘, TIMESTAMP ‘2001-02-16 20:38:40‘);
Result: 16
SELECT date_part(‘hour‘, INTERVAL ‘4 hours 3 minutes‘);
Result: 4
时间: 2024-07-30 13:49:17

postgres的强制类型转换与时间函数的相关文章

JS 数据类型转换-转换函数、强制类型转换、利用js变量弱类型转换

1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把值转换成浮点数.只有对String类型调用这些方法,这两个函数才能正确运行:对其他类型返回的都是NaN(Not a Number). 在判断字符串是否是数字值前,parseInt()和parseFloat()都会仔细分析该字符串.parseInt()方法首先查看位置0处的 字符,判断它是否是个有效数字:如果不是,该方法将返回NaN,不再继续执行其他操作.但如果该字符是有效数字,该方法

C++解析(25):关于动态内存分配、虚函数和继承中强制类型转换的疑问

0.目录 1.动态内存分配 1.1 new和malloc的区别 1.2 delete和free的区别 2.虚函数 2.1 构造函数与析构函数是否可以成为虚函数? 2.2 构造函数与析构函数是否可以发生多态? 3.继承中的强制类型转换 4.小结 1.动态内存分配 1.1 new和malloc的区别 new关键字与malloc函数的区别: new关键字是C++的一部分 malloc是由C库提供的函数 new以具体类型为单位进行内存分配 malloc以字节为单位进行内存分配 new在申请内存空间时可进

由几道JS笔试题引发的知识点探究二——强制类型转换

强制类型转换的概念相信大家一定不陌生,例如整数和浮点数进行算术运算,整数会在后台转型为浮点数.JS作为一门弱类型的动态脚本语言,任何两种数据类型之间都可以进行性转换而不会报错,这就带来了一整套错综复杂的类型转换规则.例如我们的题目 alert('5'+5),大家都知道答案是'55',但为什么这里不将string转换成number而要将number转换成string呢?在其他情况下也都要将string转型成number吗?下面我们就来做一次完整的总结. 一.何时转型为boolean? 1. 逻辑非

JAVA强制类型转换(转载+自己的感想) - stemon

JAVA强制类型转换(转载+自己的感想) - stemon 时间 2013-10-29 15:52:00  博客园-Java原文  http://www.cnblogs.com/stemon/p/3394464.html 首先声明:这篇文章的大部分是转载的,但是又有自己增加的部分,觉得这样才完整.我增加的部分只是自己的个人见解,推荐出来希望能得到大神的指正.再次说明我推荐出来是讨论的,虽然我潜水很久了,我依旧是菜鸟一枚. 在java中强制类型转换分为基本数据类型和 引用数据类型 两种,这里我们讨

Python 强制类型转换

学习过程中遇到了强转问题,这里整理一下. 前言 本篇主要介绍Python的强制类型转换. 软件环境 系统 UbuntuKylin 14.04 软件 Python 2.7.3 IPython 4.0.0 Python数据类型的显式转换 数据类型的显示转换,也称为数据类型的强制类型转换,是通过Python的内建函数来实现的类型转换. 显式转换的多种类型 int(x [,base]) ? 将x转换为一个十进制的整数 long(x [,base]) ? 将x转换为一个十进制的长整数 float(x) ?

C++强制类型转换操作符 const_cast

const_cast也是一个强制类型转换操作符.<C++ Primer>中是这样描述它的: 1.将转换掉表达式的const性质. 2.只有使用const_cast才能将const性质性质转化掉.试图使用其他三种形式的强制转换都会导致编译时的错误.(添加const还可以用其他转换符,如static_const) 3.除了添加const或删除const特性,使用const_cast符来执行其他任何类型的转换都会引起编译错误.(volatile限定符也包括,不过我不怎么了解,本文主要说const)

C++中的向上类型转换和向下类型转换+四种强制类型转换

转自博客:http://blog.csdn.net/wangweitingaabbcc/article/details/7720979# 在c++的世界中有这样两个概念,向上类型转换,向下类型转换,分别描述的是子类向基类,和基类向子类的强制类型转换. 向上强制类型转换 切割:覆盖方法和子类数据丢失的现象生成切割(slice) class Base { public: int b; virtual void Test() { cout << "base" <<en

C++强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast

1. c强制转换与c++强制转换 c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1 type-id(expression)//转换格式2 c++除了能使用c语言的强制类型转换外,还新增了四种强制类型转换:static_cast.dynamic_cast.const_cast.reinterpret_cast,主要运用于继承关系类间的强制转化,语法为: static_cast<new_type> (expression) dynam

js类型转换-字符串转整型、浮点型方法、强制类型转换等

1. 转换函数: js 提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把值转换成浮点数.只有对String类型调用这些方法, 这两个函数才能正确运行:对其他类型返回的都是NaN(Not a Number).这两个转换函数的结果都是将String数据类型转化为Number. 在 判断字符串是否是数字值前,parseInt()和parseFloat()都会仔细分析该字符串.parseInt()方法首先查看位置0处的 字符,判断它是否是个有效数字:如果不是,