HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

Function extract() from date returns double precision value

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
precisionfunctiondatevaluedoublereturnsextractfrom

Problem

Why does the Postgres function extract() return double precision from date value?

Only reason I could imagine is, there are some locales using calendars (those not Gregorian calendars and supported by Postgres), calculate date/time differently and possible return float number.

Solution

Because the return values can be large or a decimal number.

Double precision will accept a wide range of values

Looking at other numeric types, you only have decimal which will have overhead: you don't know the return scale or precision needed beforehand so it would have to wide

SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
Result: 28.5

SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
Result: 982384720.12


See PG docs

Code Snippets

SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
Result: 28.5

SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40.12-08');
Result: 982384720.12

Context

StackExchange Database Administrators Q#84305, answer score: 5

Revisions (0)

No revisions yet.