Author Avatar

admin

0

Share post:

PostgreSQL supports all types of date, time, and timestamp data types, and operations on them.

Date/Time Types

NameStorage SizeDescriptionLow ValueHigh ValueResolution
timestamp [ (p) ] [ without time zone ]8 bytesboth date and time (no time zone)4713 BC294276 AD1 microsecond
timestamp [ (p) ] with time zone8 bytesboth date and time, with time zone4713 BC294276 AD1 microsecond
date4 bytesdate (no time of day)4713 BC5874897 AD1 day
time [ (p) ] [ without time zone ]8 bytestime of day (no date)00:00:0024:00:001 microsecond
time [ (p) ] with time zone12 bytestime of day (no date), with time zone00:00:00+155924:00:00-15591 microsecond
interval [ fields ] [ (p) ]16 bytestime interval-178000000 years178000000 years1 microsecond

Date and time input is accepted in almost any reasonable format, including ISO 8601, SQL-compatible, traditional POSTGRES, and others. If there is any ambiguity, then the DateStyle parameter can be set to a required format (MDY or DMY, or YMD).

Date Input

Parameter DateStyle dictates the input date format. Check the current DateStyle format.

show datestyle;

It dictates that the input date should be in format month-date-year format or any of its close variations but in the same order. Trying to input the date in some other order will give an error. Let’s try these commands:

select '20/12/2016'::date; -- error

select '12/20/2016'::date; -- correct

select '12-20-2016'::date; -- correct

select '12:20:2016'::date; -- error

Parameter DateStyle can be changed at the session level, database level, or instance level.

Session Level

At the session level, it can be changed by setting the environment variable PGDATESTYLE or SET command.

  • Using Environment Variable
export PGDATESTYLE="ISO, DMY";

psql -p 5433

select '20/12/2016'::date;
  • Using SET command
show datestyle;

SET datestyle TO "ISO, DMY";

show datestyle;

select '20/12/2016'::date;

select '12/20/2016'::date;

There are other options as well for SET datestyle command.

SET DATESTYLE TO Postgres, MDY;

SET DATESTYLE TO SQL;

SET DATESTYLE TO German;
Database Level

Parameter Datestyle can be changed at the individual database level inside a cluster using ALTER DATABASE command. But, to bring the change into effect, the whole cluster has to be restarted.

Don’t forget to unset PGDATESTYLE as it overrides the database/instance level value of parameter DateStyle.

\c appdb

alter database appdb set datestyle TO "ISO, DMY";

\q
pg_ctlcluster 13 mars stop

pg_ctlcluster 13 mars start

unset PGDATESTYLE

psql -p 5433

select '20/12/2016'::date;

select '12/20/2016'::date;
Instance Level

Parameter Datestyle can be changed at the instance level by changing the parameter in ‘postgresql.conf’ file followed by a restart of the cluster.

show datestyle;

select now();

alter system set datestyle='German';

pg_ctlcluster 13 mars stop

pg_ctlcluster 13 mars start

unset PGDATESTYLE

psql -p 5433

select '20/12/2016'::date;

select '12/20/2016'::date;

Time Input

Time can be input in any of the general formats.

select '10:00:00'::time;

select '22:00:00'::time;

select '10:00:00 AM'::time;

select '10:00:00 PM'::time;

Timestamp Input

Timestamp can be input in any of the general formats.

select '31.03.2017 09:30:20'::timestamp;

select '31.03.2017 19:30:20'::timestamp;

select '31-03-2017 19:30:20'::timestamp;

select '31-03-2017 11:30:20 AM'::timestamp;

select '31-03-2017 11:30:20 PM'::timestamp;

Normal strings can be converted to date or systimestamp using in-built functions, and vice versa. Let’s see those in action:

Template Patterns for Date/Time Formatting

PatternDescription
HHhour of day (01-12)
HH12hour of day (01-12)
HH24hour of day (00-23)
MIminute (00-59)
SSsecond (00-59)
MSmillisecond (000-999)
USmicrosecond (000000-999999)
SSSSseconds past midnight (0-86399)
AMamPM or pmmeridiem indicator (without periods)
A.M.a.m.P.M. or p.m.meridiem indicator (with periods)
Y,YYYyear (4 or more digits) with comma
YYYYyear (4 or more digits)
YYYlast 3 digits of year
YYlast 2 digits of year
Ylast digit of year
IYYYISO 8601 week-numbering year (4 or more digits)
IYYlast 3 digits of ISO 8601 week-numbering year
IYlast 2 digits of ISO 8601 week-numbering year
Ilast digit of ISO 8601 week-numbering year
BCbcAD or adera indicator (without periods)
B.C.b.c.A.D. or a.d.era indicator (with periods)
MONTHfull upper case month name (blank-padded to 9 chars)
Monthfull capitalized month name (blank-padded to 9 chars)
monthfull lower case month name (blank-padded to 9 chars)
MONabbreviated upper case month name (3 chars in English, localized lengths vary)
Monabbreviated capitalized month name (3 chars in English, localized lengths vary)
monabbreviated lower case month name (3 chars in English, localized lengths vary)
MMmonth number (01-12)
DAYfull upper case day name (blank-padded to 9 chars)
Dayfull capitalized day name (blank-padded to 9 chars)
dayfull lower case day name (blank-padded to 9 chars)
DYabbreviated upper case day name (3 chars in English, localized lengths vary)
Dyabbreviated capitalized day name (3 chars in English, localized lengths vary)
dyabbreviated lower case day name (3 chars in English, localized lengths vary)
DDDday of year (001-366)
IDDDday of ISO 8601 week-numbering year (001-371; day 1 of the year is Monday of the first ISO week)
DDday of month (01-31)
Dday of the week, Sunday (1) to Saturday (7)
IDISO 8601 day of the week, Monday (1) to Sunday (7)
Wweek of month (1-5) (the first week starts on the first day of the month)
WWweek number of year (1-53) (the first week starts on the first day of the year)
IWweek number of ISO 8601 week-numbering year (01-53; the first Thursday of the year is in week 1)
CCcentury (2 digits) (the twenty-first century starts on 2001-01-01)
JJulian Date (integer days since November 24, 4714 BC at local midnight; see Section B.7)
Qquarter
RMmonth in upper case Roman numerals (I-XII; I=January)
rmmonth in lower case Roman numerals (i-xii; i=January)
TZupper case time-zone abbreviation (only supported in to_char)
tzlower case time-zone abbreviation (only supported in to_char)
OFtime-zone offset from UTC (only supported in to_char)

Converting string to date/timestamp

select to_date('2017-04-03', 'YYYY-MM-DD');

select to_date('2017-04-03 10:00:00 AM', 'YYYY-MM-DD');

select to_date('2017-04-03 10:00:00 AM', 'YYYY-MM-DD HH24:MI:SS');

select to_date('2017-04-03', 'YYYY-MM-DD HH24:MI:SS');

select to_timestamp('2017-04-03', 'YYYY-MM-DD');

select to_timestamp('2017-04-03 10:00:00 AM', 'YYYY-MM-DD');

select to_timestamp('2017-04-03 10:00:00 AM', 'YYYY-MM-DD HH24:MI:SS');

select to_timestamp('2017-04-03', 'YYYY-MM-DD HH24:MI:SS');

--MS is millisecond (000-999), will take last 3 digits from the millisecond input field.
select to_timestamp('2017-04-03 10:00:00:0000120 AM', 'YYYY-MM-DD HH24:MI:SS:MS');

--US is microsecond (000000-999999), will take last 6 digits from the millisecond input field.
select to_timestamp('2017-04-03 10:00:00:00234120 AM', 'YYYY-MM-DD HH24:MI:SS:US');

Put above statements into a file date.sql and run on ‘psql’ prompt. The output would be as below:

postgres=# \i date.sql
  to_date
------------
 03.04.2017
(1 row)

  to_date
------------
 03.04.2017
(1 row)

  to_date
------------
 03.04.2017
(1 row)

  to_date
------------
 03.04.2017
(1 row)

      to_timestamp
-------------------------
 03.04.2017 00:00:00 IST
(1 row)

      to_timestamp
-------------------------
 03.04.2017 00:00:00 IST
(1 row)

      to_timestamp
-------------------------
 03.04.2017 10:00:00 IST
(1 row)

      to_timestamp
-------------------------
 03.04.2017 00:00:00 IST
(1 row)

        to_timestamp
----------------------------
 03.04.2017 10:00:00.12 IST
(1 row)

         to_timestamp
-------------------------------
 03.04.2017 10:00:00.23412 IST
(1 row)

postgres=#

Converting date/timestamp to string

select to_date('2017-04-03', 'YYYY-MM-DD');

select to_date('2017-04-03 10:00:00 AM', 'YYYY-MM-DD');

select to_date('2017-04-03 10:00:00 AM', 'YYYY-MM-DD HH24:MI:SS');

select to_date('2017-04-03', 'YYYY-MM-DD HH24:MI:SS');

select to_timestamp('2017-04-03', 'YYYY-MM-DD');

select to_timestamp('2017-04-03 10:00:00 AM', 'YYYY-MM-DD');

select to_timestamp('2017-04-03 10:00:00 AM', 'YYYY-MM-DD HH24:MI:SS');

select to_timestamp('2017-04-03', 'YYYY-MM-DD HH24:MI:SS');

--MS is millisecond (000-999), will take last 3 digits from the millisecond input field.
select to_timestamp('2017-04-03 10:00:00:0000120 AM', 'YYYY-MM-DD HH24:MI:SS:MS');

--US is microsecond (000000-999999), will take last 6 digits from the millisecond input field.
select to_timestamp('2017-04-03 10:00:00:00234120 AM', 'YYYY-MM-DD HH24:MI:SS:US');
Title 1

Detail 1

Sub title 1

Sub title 2

Sub Detail 1

Title 2

ggggggg

dadadsasda

Title 3

Detail 3

Title 4

Detail 4

Casting of Data Types
Creating Schemas

Leave a Comment

Your email address will not be published. Required fields are marked *