FORMAT¶
The FORMAT function in SQL is used to format date, time, and timestamp values into a specified format.
Syntax¶
value: The date, time, or timestamp value that is to be formatted.format: The format in which the value is to be displayed. This is a string that contains format specifiers, such as%Yfor four-digit year,%mfor two-digit month, and so on.
Usage¶
Here are examples of how FORMAT can be used to display datetime components in various formats:
-
Formatting a
This returnsDATEvalue:"2017-06". -
Formatting a
This returnsTIMESTAMPvalue:"2015-09-05 23:56:04". -
Formatting a
This returnsTIMEvalue:"23:56". -
Formatting different components of a
This returns:TIMESTAMPvalue separately:
Please note that the FORMAT function only accepts date, time, or timestamp values. If you try to format a value with an incorrect type, you will encounter an error.
Error Example¶
This will throw an error because the input value is a string, not a date, time, or timestamp value: