New versions: Dev (3.14) | Latest (3.13) | 3.12

CENTURY

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

Extract the CENTURY value from a datetime value.

The CENTURY function is a short version of the EXTRACT, passing a DatePart.CENTURY value as an argument.

SELECT century(DATE '2020-02-03');
create.select(century(Date.valueOf("2020-02-03"))).fetch();

The result being

+---------+
| century |
+---------+
|      21 |
+---------+

Dialect support

This example using jOOQ:

century(Date.valueOf("2020-02-03"))

Translates to the following dialect specific expressions:

-- ACCESS
(cdec(((sgn(datepart('yyyy', #2020/02/03 00:00:00#)) * (abs(datepart('yyyy', #2020/02/03 00:00:00#)) + 99)) / 100)) - (((sgn(datepart('yyyy', #2020/02/03 00:00:00#)) * (abs(datepart('yyyy', #2020/02/03 00:00:00#)) + 99)) / 100) < cdec(((sgn(datepart('yyyy', #2020/02/03 00:00:00#)) * (abs(datepart('yyyy', #2020/02/03 00:00:00#)) + 99)) / 100))))

-- ASE, SYBASE
floor(((sign(datepart(yy, '2020-02-03 00:00:00.0')) * (abs(datepart(yy, '2020-02-03 00:00:00.0')) + 99)) / 100))

-- AURORA_MYSQL, MEMSQL, MYSQL
floor(((sign(extract(YEAR FROM {ts '2020-02-03 00:00:00.0'})) * (abs(extract(YEAR FROM {ts '2020-02-03 00:00:00.0'})) + 99)) / 100))

-- AURORA_POSTGRES, POSTGRES
extract(CENTURY FROM TIMESTAMP '2020-02-03 00:00:00.0')

-- COCKROACHDB, FIREBIRD, H2, HANA, HSQLDB, INGRES, MARIADB, ORACLE, REDSHIFT, TERADATA, VERTICA
floor(((sign(extract(YEAR FROM TIMESTAMP '2020-02-03 00:00:00.0')) * (abs(extract(YEAR FROM TIMESTAMP '2020-02-03 00:00:00.0')) + 99)) / 100))

-- CUBRID
floor(((sign(extract(YEAR FROM DATETIME '2020-02-03 00:00:00.0')) * (abs(extract(YEAR FROM DATETIME '2020-02-03 00:00:00.0')) + 99)) / 100))

-- DB2
floor(((sign(YEAR(TIMESTAMP '2020-02-03 00:00:00.0')) * (abs(YEAR(TIMESTAMP '2020-02-03 00:00:00.0')) + 99)) / 100))

-- DERBY
floor(((sign(YEAR(TIMESTAMP('2020-02-03 00:00:00.0'))) * (abs(YEAR(TIMESTAMP('2020-02-03 00:00:00.0'))) + 99)) / 100))

-- INFORMIX
floor(((sign(YEAR(DATETIME(2020-02-03 00:00:00.0) YEAR TO FRACTION)) * (abs(YEAR(DATETIME(2020-02-03 00:00:00.0) YEAR TO FRACTION)) + 99)) / 100))

-- SQLDATAWAREHOUSE, SQLSERVER
floor(((sign(datepart(yy, CAST('2020-02-03 00:00:00.0' AS DATETIME2))) * (abs(datepart(yy, CAST('2020-02-03 00:00:00.0' AS DATETIME2))) + 99)) / 100))

-- SQLITE
(CAST(((CASE WHEN strftime('%Y', '2020-02-03 00:00:00.0') > 0 THEN 1 WHEN strftime('%Y', '2020-02-03 00:00:00.0') < 0 THEN -1 WHEN strftime('%Y', '2020-02-03 00:00:00.0') = 0 THEN 0 END * (abs(strftime('%Y', '2020-02-03 00:00:00.0')) + 99)) / 100) AS int8) - (((CASE WHEN strftime('%Y', '2020-02-03 00:00:00.0') > 0 THEN 1 WHEN strftime('%Y', '2020-02-03 00:00:00.0') < 0 THEN -1 WHEN strftime('%Y', '2020-02-03 00:00:00.0') = 0 THEN 0 END * (abs(strftime('%Y', '2020-02-03 00:00:00.0')) + 99)) / 100) < CAST(((CASE WHEN strftime('%Y', '2020-02-03 00:00:00.0') > 0 THEN 1 WHEN strftime('%Y', '2020-02-03 00:00:00.0') < 0 THEN -1 WHEN strftime('%Y', '2020-02-03 00:00:00.0') = 0 THEN 0 END * (abs(strftime('%Y', '2020-02-03 00:00:00.0')) + 99)) / 100) AS int8)))

(These are currently generated with jOOQ 3.14, see #10141)

The jOOQ Logo