T-SQL 101: #65 Formatting strings in SQL Server T-SQL using FORMAT

In upcoming blog posts, I’ll show you how to change from one data type to another. I’ll show you how to use CONVERT to output date/time values to string formats.

If you look at the web page for CAST and CONVERT, you’ll notice there are many styles for conversion. For example, the US format is called 101 if it has a 4 digit century. But it's called 1 if you only have a 2 digit century. Another very common one is the British/French one and that’s 103 for a 4 digit century or just 3 for a 2 digit century.

However, I’ve found that developers want more control over string conversions for dates and times. The FORMAT function is a wrapper around the .NET framework format function and allows you to do .NET types of formatting.

Look at the following query:

And see the results here:

Note that the formats are expressed as .NET format strings.

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.