These built-in functions are used to manipulate data formats.
CAST: Explicitly converts an expression from one data type to another.
SYNTAX:
CAST ( expression AS [data type] )
EXAMPLE
Query:
SELECT CAST( 9.2 AS INTEGER) AS [CAST]
Output :
CAST
9
CONVERT: Similar to CAST, COVERT also explicitly converts an expression from one data type to another.
SYNTAX:
CONVERT ( data type , expression [, style] )
EXAMPLE 1:
Query:
SELECT CONVERT(VARCHAR(30 ), GETDATE()) AS [Default Style]
Output :
Default Style
Sep 22 2024 12:28PM
EXAMPLE 2:
Query:
SELECT CONVERT(VARCHAR(30 ), GETDATE(), 1) AS [Without Century]
Output :
Without Century
09/22/24
EXAMPLE 3:
Query:
SELECT CONVERT(VARCHAR(30 ), GETDATE(), 101) AS [With Century]
Output :
With Century
09/22/2024