Skip to content

CAST to DATE gives text with a two-digit year: CAST('2020-01-02' AS DATE) is '20.01.02' #2804

Open

Description

@mathiasrw

CAST(x AS DATE) gives text with a two-digit year and dots: CAST('2020-01-02' AS DATE) is '20.01.02'. CONVERT(DATE, ...) gives the same. An invalid date rolls over: CAST('1994-02-30' AS DATE) is '94.03.02', where SQL-92 requires an error (SQLSTATE 22007, invalid datetime format). CAST(... AS DATETIME) gives '2020.01.02 11:00:00.000' in a time zone of UTC+11, so the time depends on the machine.

CAST(... AS TIME) and CAST(... AS TIMESTAMP) keep the text as written.

Seen in the NIST SQL Test Suite run in dml106.sql and dml121.sql (INSERT INTO LOTSA_DATETIMES VALUES (CAST ('1976-06-21' AS DATE), ...), INSERT INTO WOODCHUCK VALUES (CAST ('1994-02-30' AS DATE), NULL)). Reproduced on develop (38bf3c3):

alasql("SELECT CAST('2020-01-02' AS DATE) AS d");            // [{d: '20.01.02'}]
alasql("SELECT CONVERT(DATE, '2020-01-02') AS d");           // [{d: '20.01.02'}]
alasql("SELECT CAST('1994-02-30' AS DATE) AS d");            // [{d: '94.03.02'}]
alasql("SELECT CAST('2020-01-02' AS DATETIME) AS d");        // [{d: '2020.01.02 11:00:00.000'}]
alasql("SELECT CAST('1927-11-30 07:10:00' AS TIMESTAMP) AS d");   // [{d: '1927-11-30 07:10:00'}]

Expected: CAST('2020-01-02' AS DATE) is the date 2020-01-02 (written as '2020-01-02' if it stays text), and CAST('1994-02-30' AS DATE) is an error.

Breaking?

Yes. The result of CAST(... AS DATE) changes, and code can compare or display the '20.01.02' text it gets today.

Related: #1627, #673.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions