Teradata SQL - TRYCAST / TRY_CAST Function

Raymond Raymond event 2020-03-15 visibility 3,760
more_vert

In SQL Server, TRY_CAST function 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

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts