Jump to: navigation, search

Postgresql Functions to work with Date and Time

From w3cyberlearnings

Contents

DATE Function for the past

test2=# SELECT DATE (CURRENT_DATE);
    date    
------------
 2012-01-31
(1 row)

test2=# SELECT DATE (CURRENT_DATE - integer '1');
    date    
------------
 2012-01-30
(1 row)

test2=# SELECT DATE (CURRENT_DATE - integer '3');
    date    
------------
 2012-01-28
(1 row)

test2=# SELECT DATE (CURRENT_DATE - integer '30');
    date    
------------
 2012-01-01

Date function for the future

test2=# SELECT CURRENT_DATE;
    date    
------------
 2012-01-31
(1 row)

test2=# SELECT DATE( CURRENT_DATE + integer '1');
    date    
------------
 2012-02-01
(1 row)

test2=# SELECT DATE( CURRENT_DATE + integer '7');
    date    
------------
 2012-02-07
(1 row)

test2=# SELECT DATE( CURRENT_DATE + integer '10');
    date    
------------
 2012-02-10
(1 row)

Date for the past with time

Take away 24:00 hour or take away one day

test2=# SELECT DATE(CURRENT_DATE - integer '1' );
    date    
------------
 2012-01-30
(1 row)

test2=# SELECT DATE(CURRENT_DATE - time '24:00');
    date    
------------
 2012-01-30
(1 row)
Navigation
Web
SQL
MISC
References