Forcing a FORMAT on CAST: Converting Character to DATE | VantageCloud Lake - Forcing a FORMAT on CAST for Converting Character to DATE - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
ft:locale
en-US
ft:lastEdition
2024-12-11
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

You can use a FORMAT phrase to convert a character string that does not match the format of the target DATE data type. A character string in a conversion that does not specify a FORMAT phrase uses the output format for the DATE data type.

For example, suppose the session dateform is INTEGERDATE and the default DATE format of the system is set to 'yyyymmdd'. The following statement fails, because the character string contains separators, which does not match the default DATE format:

SELECT CAST ('2005-01-01' AS DATE);

To override the default DATE format, and convert a character string that contains separators, specify a FORMAT phrase for the DATE target type:

SELECT CAST ('2005-01-01' AS DATE FORMAT 'YYYY-MM-DD');

In character-to-DATE conversions, the FORMAT phrase must not consist solely of the following formatting characters.

  • EEEE
  • E4
  • EEE
  • E3

Separator Characters

If the FORMAT phrase has no separator, the character value cannot have one either. For example:

SELECT CAST ('20050101' AS DATE FORMAT 'YYYYMMDD');
'20050101'
----------
  20050101
SELECT CAST ('050101' AS DATE FORMAT 'YYMMDD');
'050101'
--------
  050101

If the FORMAT phrase has a separator, the character value must also have one (but not necessarily the same one). For example:

SELECT CAST ('2005+01+01' AS DATE FORMAT 'YY/MM/DD');
'2005+01+01'
------------
    05/01/01