PostgreSQL peut indiquer le siècle d’une date grâce à to_char(). Utilisez CC comme second argument : il renvoie le siècle sur deux chiffres.
Exemple
SELECT to_char(date '2001-03-20', 'CC');
Résultat :
21
Le 21e siècle commence le 1er janvier 2001. Une date antérieure relève donc d’un autre siècle.
SELECT to_char(date '2000-03-20', 'CC');
Résultat :
20
Ajouter le suffixe ordinal
Ajoutez th ou TH :
SELECT to_char(date '2001-03-20', 'CCth') AS "2001 CCth", to_char(date '2001-03-20', 'CCTH') AS "2001 CCTH", to_char(date '2000-03-20', 'CCth') AS "2000 CCth", to_char(date '2000-03-20', 'CCTH') AS "2000 CCTH";
Résultat :
2001 CCth | 2001 CCTH | 2000 CCth | 2000 CCTH -----------+-----------+-----------+----------- 21st | 21ST | 20th | 20TH
Ajouter « Century »
SELECT concat(to_char(date '2001-03-20', 'CCth'), ' Century!');
Résultat :
21st Century!