Example and Notes
Use this SQL Server example as a starting point for development, troubleshooting, reporting, or maintenance work. Review it against your schema, data volume, permissions, and SQL Server version before using it in production.
The Format Date function is a powerful SQL function you can use to manipulate and work with dates. For example, you may want to format a date to display it in a number of various ways. The example scripts below will show you how to format a date for display in your query results. Note - the format function only works in SQL Server 2008 +
Below is a list of formats and an example of the output. The date used for
all of these examples is "2018-11-07 11:36:14.840".
| Query |
Sample output |
| SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date |
07/11/2018 |
| SELECT FORMAT (getdate(), 'dd/MM/yyyy, hh:mm:ss ') as date |
07/11/2018, 11:36:14 |
| SELECT FORMAT (getdate(), 'dddd, MMMM, yyyy') as date |
Wednesday, November, 2018 |
| SELECT FORMAT (getdate(), 'MMM dd yyyy') as date |
Nov 07 2018 |
| SELECT FORMAT (getdate(), 'MM.dd.yy') as date |
11.07.18 |
| SELECT FORMAT (getdate(), 'MM-dd-yy') as date |
11-07-18 |
| SELECT FORMAT (getdate(), 'hh:mm:ss tt') as date |
11:36:14 AM |
| SELECT FORMAT (getdate(), 'd','us') as date |
11/07/2018 |
Production Review
WSI can adapt this script for your database, improve error handling, tune performance, document the logic, and help deploy it safely.