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
Nov 23 2024 8:29PM
EXAMPLE 2:
Query:
SELECT CONVERT(VARCHAR(30 ), GETDATE(), 1) AS [Without Century]
Output :
Without Century
11/23/24
EXAMPLE 3:
Query:
SELECT CONVERT(VARCHAR(30 ), GETDATE(), 101) AS [With Century]
Output :
With Century
11/23/2024