Teradata SQL - TRYCAST / TRY_CAST Function

Raymond Tang Raymond Tang 0 4079 2.11 index 3/15/2020

In SQL Server, TRY_CASTfunction returns a value cast to a specified data type is the cast is successful or null is the cast is not successful.  In Teradata, the equivalent function is TRYCAST.

Code snippets

-- return int 1030
SELECT TRYCAST('01030' AS int);
-- return 2019-01-01 as it is a valid date string 
SELECT TRYCAST('2019-01-01' AS DATE);
-- return null 
SELECT TRYCAST('2019-13-32' AS DATE);
SELECT TRYCAST('-128' AS byteint);
SELECT TRYCAST('127' AS byteint);
-- return null as overflow
SELECT TRYCAST('128' AS byteint);

Note

In SQL Server, the first argument can be any valid expression while in Teradata, it needs to be CHAR or VARCHAR.

Teradata TRY_CAST

Teradata TRY_CONVERT

Teradata CAST test

sql teradata teradata-functions

Join the Discussion

View or add your thoughts below

Comments