Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Postgres FD Implementation
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abuhujair Javed
Postgres FD Implementation
Commits
1c208576
Commit
1c208576
authored
Sep 03, 2013
by
Bruce Momjian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Docs: add paragraph about date/timestamp subtraction
per suggestion from Francisco Olart
parent
9d323bda
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
1 deletion
+31
-1
doc/src/sgml/func.sgml
doc/src/sgml/func.sgml
+31
-1
No files found.
doc/src/sgml/func.sgml
View file @
1c208576
...
@@ -6431,7 +6431,7 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
...
@@ -6431,7 +6431,7 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
</entry>
</entry>
<entry><type>
interval
</type></entry>
<entry><type>
interval
</type></entry>
<entry>
Subtract arguments, producing a
<quote>
symbolic
</>
result that
<entry>
Subtract arguments, producing a
<quote>
symbolic
</>
result that
uses years and months
</entry>
uses years and months
</entry>
, rather than just days
<entry><literal>
age(timestamp '2001-04-10', timestamp '1957-06-13')
</literal></entry>
<entry><literal>
age(timestamp '2001-04-10', timestamp '1957-06-13')
</literal></entry>
<entry><literal>
43 years 9 mons 27 days
</literal></entry>
<entry><literal>
43 years 9 mons 27 days
</literal></entry>
</row>
</row>
...
@@ -6794,6 +6794,36 @@ SELECT (DATE '2001-10-30', DATE '2001-10-30') OVERLAPS
...
@@ -6794,6 +6794,36 @@ SELECT (DATE '2001-10-30', DATE '2001-10-30') OVERLAPS
days
</>
because May has 31 days, while April has only 30.
days
</>
because May has 31 days, while April has only 30.
</para>
</para>
<para>
Subtraction of dates and timestamps can also be complex. The most
accurate way to perform subtraction is to convert each value to a number
of seconds using
<literal>
EXTRACT(EPOCH FROM ...)
</>
and compute the
number of
<emphasis>
seconds
</>
between the two values. This will adjust
for the number of days in each month, timezone changes, and daylight
saving time adjustments. Operator subtraction of date or timestamp
values returns the number of days (24-hours) and hours/minutes/seconds
between the values, making the same adjustments. The
<function>
age
</>
function returns years, months, days, and hours/minutes/seconds,
performing field-by-field subtraction and then adjusting for negative
field values. The following queries, produced with
<literal>
timezone
= 'US/Eastern'
</>
and including a daylight saving time change,
illustrates these issues:
</para>
<screen>
SELECT EXTRACT(EPOCH FROM timestamptz '2013-07-01 12:00:00') -
EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00');
<lineannotation>
Result:
</lineannotation><computeroutput>
10537200
</computeroutput>
SELECT (EXTRACT(EPOCH FROM timestamptz '2013-07-01 12:00:00') -
EXTRACT(EPOCH FROM timestamptz '2013-03-01 12:00:00'))
/ 60 / 60 / 24;
<lineannotation>
Result:
</lineannotation><computeroutput>
121.958333333333
</computeroutput>
SELECT timestamptz '2013-07-01 12:00:00' - timestamptz '2013-03-01 12:00:00';
<lineannotation>
Result:
</lineannotation><computeroutput>
121 days 23:00:00
</computeroutput>
SELECT age(timestamptz '2013-07-01 12:00:00', timestamptz '2013-03-01 12:00:00');
<lineannotation>
Result:
</lineannotation><computeroutput>
4 mons
</computeroutput>
</screen>
<sect2
id=
"functions-datetime-extract"
>
<sect2
id=
"functions-datetime-extract"
>
<title><function>
EXTRACT
</function>
,
<function>
date_part
</function></title>
<title><function>
EXTRACT
</function>
,
<function>
date_part
</function></title>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment