DATE_FORMAT
Description
The DATE_FORMAT
function is used to convert different types of time expressions (such as strings, timestamps, etc.) into date strings according to a specified format.
Parameter Description
- expr: Can be an expression of types such as string, timestamp, timestamp with timezone, etc.
- fmt: A string used to describe the target date format. For specific formats, refer to the DateTimePatterns page.
Return Type
Returns a date string representing the date converted according to the specified format.
Example
- Convert a string type time expression to a date string:
- Convert the current timestamp to a date string:
- Convert the current timestamp to a date string with week information:
Notes
- When the format of the input time expression does not match the actual type, it may lead to conversion failure.
- When using the
DATE_FORMAT
function, please ensure that the format of thefmt
parameter is correct to avoid erroneous output results.
Through the above examples and explanations, you can flexibly use the DATE_FORMAT
function to format time expressions to meet your needs in actual business scenarios.