A. Type Conversions
Postgres type conversions: generally:: Used for type conversion, timestamp to date more
Select Now ()::d ate
Select Now ():: varchar
Example 1: VarChar of the date is calculated as date
Select ' 2012-11-15 16:15:56.377000+08 ':: Timestamp::d ate
Select ' 2012-11-15 16:15:56.377000+08 '::d ate
Results: 2012-11-15
two. Type conversion and relative time of time
Note that Java's timestamp future in SQL embodies the form of varchar ' 2012-11-15 16:15:56.377000+08 ', such that the string can calculate the time difference.
If the publishdate of a record in the table is ' 2012-11-15 16:15:56.377000+08 ', to confirm that the record is not publish within the last 24 hours, you can use the following judgment:
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 ' < ' 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 ()::d ate-' 2012-11-15 16:15:56.377000+08 '::d ate < 1
Note: The relative time represents the time range, which is usually used for statistical, scheduled tasks. In addition to the relative time, ' Today ' is used more. For example, take the day's records using: publishdate::d ate = ' Today '
three. Time function extract for extracting absolute time of year, month, day ...; The second value of the relative time.
EXTRACT (fieldFromsource)
The extract function retrieves subfields such as year or hour from Date/time values. source must is a value expression of Typ E timestamp , time , or interval . (Expressions of type would be cast to and date timestamp can therefore are used as well.) are an field identifier or a string that Selects what field to extract is from the source value. The extract function returns the values of double precision type. The following is valid field names:
-
-
century
-
-
The Century
SELECT EXTRACT (CENTURY from TIMESTAMP ' 2000-12-16 12:21:13 '); Result:20select 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 does not know it at the time. This definition applies the Gregorian calendar countries. There is no century number 0, you go from-1 to 1. PostgreSQL releases before 8.0 didn't follow the conventional numbering of centuries, but just returned the year field di vided 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
timestamp values only)
SELECT EXTRACT (DOW from TIMESTAMP ' 2001-02-16 20:38:40 '); Result:5
Note that's day of the week numbering was different from the the extract to_char function.
-
-
doy
-
The day's
-
(1--365/366) (for values only
timestamp )
SELECT EXTRACT (Doy from TIMESTAMP ' 2001-02-16 20:38:40 '); Result:47
-
-
epoch
-
-
date timestamp for and values, the number of seconds since 1970-01-01 00:00:00-00 (can is negative); interval for values, t He total number of seconds in the interval
SELECT EXTRACT (EPOCH from TIMESTAMP with time ZONE ' 2001-02-16 20:38:40-08 '); Result:982384720select EXTRACT (EPOCH from INTERVAL ' 5 days 3 hours '); result:442800
Here's how can I 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 is includes full seconds.
SELECT EXTRACT (microseconds from time ' 17:12:28.5 '); result:28500000
-
-
millennium
-
The
-
Millennium
SELECT EXTRACT (MILLENNIUM from TIMESTAMP ' 2001-02-1620:38:40 '); Result:3
Years in the 1900s is in the second millennium. The third millennium starts January 1, 2001. PostgreSQL releases before 8.0 didn't follow the conventional numbering of millennia, but just returned the year field di vided by 1000.
-
-
milliseconds
-
The
-
seconds field, including fractional parts, multiplied by 1000. Note that this is 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
-
-
timestamp for values, the number of the month within the year (1--12); For values the number of interval months, modulo 12 (0--11)
SELECT EXTRACT (MONTH from TIMESTAMP ' 2001-02-16 20:38:40 '); Result:2select EXTRACT (MONTH from INTERVAL ' 2 years 3 months '); Result:3select EXTRACT (MONTH from INTERVAL ' 2 years months '); Result:1
-
-
quarter
-
The quarter of the year (1--4) that's the day was in
-
(for
timestamp values 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:40select EXTRACT (SECOND from time ' 17:12:28.5 '); result:28.5
-
-
timezone
-
-
The time zone is 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 in. By definition (ISO 8601), the first week of a year contains January 4 of this year. (The ISO-8601 week starts on Monday.) In other words, the first Thursday of an year are in Week 1 of this year. (
timestamp for values only) Because of this, it's possible for early January dates to being part of the 52nd or 53rd week of T He previous year. For example, was part of 2005-01-01 the 53rd week of year 2004, and was part of the 2006-01-01 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 are no
0 AD , so subtracting BC years from years should are done with care AD .
SELECT EXTRACT (Year from TIMESTAMP ' 2001-02-16 20:38:40 '); result:2001
The extract function is a primarily intended for computational processing. For formatting date/time the values for display, see section 7.8 Data Type formatting Functions.
The date_part function is modeled the traditional Ingres equivalent to the Sql-standard function extract :
Date_part (' field source )
Note field that here the parameter needs to is a string value, not a name. The valid field names for is the date_part same as for extract .
SELECT Date_part (' Day ', TIMESTAMP ' 2001-02-16 20:38:40 '); Result:16select Date_part (' Hour ', INTERVAL ' 4 hours 3 minutes '); Result:4
Coercion type conversion and time function of Postgres