T-SQL 101: #78 Custom formatting dates and times in SQL Server using FORMAT

In some upcoming posts, I'll discuss how you can change between data types, but I wanted to show you first, how you can use the FORMAT() function to convert dates and times to strings.

This query asks for the current date and time value formatted as day then month, then (four digit) year. It also includes the desired culture (en-US) which would be used if the format required any culture-specific items, such as the names of months.

You might wonder where that format string ddMMyyyy comes from. That's actually a .NET standard format, and so this is actually a wrapper function that calls the .NET Framework under the covers.

For this reason, it's also not a very fast function. If you can use CAST and/or CONVERT to do the formatting, always use that instead.

Learning T-SQL

It's worth your while becoming proficient in SQL. If you'd like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.

Leave a Reply

Your email address will not be published. Required fields are marked *