Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
544 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andrew Gould
In this tutorial
In applications like Microsoft Excel and Microsoft Access formatting dates and times is usually just a case of selecting an option from a list. Unfortunately, in SQL Server you have to calculate a date format using functions!
The Format function provides the easiest way to format a date in SQL. Here's an example:
SELECT
Title,
-- format the release date
FORMAT(ReleaseDate,'dddd dd MMMM, yyyy')
AS WhenReleased
FROM
Film
ORDER BY
WhenReleased ASC
This is the sort of output this would give:
The date formatting code shows the day name, day number, month name and year number.
Because we've chosen to sort by the formatted date (which is just a string of text), the dates won't be formatted in correct chronological order.
Here are the main symbols that you can use:
Symbol | What it would show |
---|---|
yy or yyyy | The year as a two-digit or four-digit number |
M, MM, MMM or MMMM | The month as a single digit number, double-digit number, three-character code or full name |
d, dd, ddd or dddd | The day as a single digit number, double-digit number, three-character code or full name |
HH or hh | The hour for the given date/time as a two-digit number, in either 24-hour or 12-hour format |
mm | The minute for the given date/time as a two-digit number |
ss | The seconds for the given date/time as a two-digt number |
There's much more that you can do with the Format function in SQL, should the above have piqued your interest!
The CONVERT function is normally used to change one data type into another but it also has the ability to format dates. Technically it does this by converting a date into one of the text data types although that isn't really too important. All you need to know is that the function has this basic structure:
CONVERT(Data type you want, Value to convert, Date style code)
The simple example below changes a range of dates into the UK standard "DD/MM/YY" format:
It's actually quite a simple function to use.
In the above example we specified that the data type was CHAR(8) because there are eight characters in the format we want to get. AnyDate is the name of the field containing the dates we want to format, and the number 3 is the style code for UK dates. If we wanted to get the year to include the century we can add 100 to the date style code, making it 103, and increase the number of characters that we want to 10:
A couple of simple tweaks and we get the century included in the year.
There is a large number of date style codes that you can use with the CONVERT function. The table below uses the 26th of January, 2012 at 12:00PM as an example to show you what each code will return.
Without century | With century | Example |
---|---|---|
- | 0 or 100 | Jan 26 2012 12:00PM |
1 | 101 | 01/26/12 or 01/26/2012 |
2 | 102 | 12.01.26 or 2012.01.26 |
3 | 103 | 26/01/12 or 26/01/2012 |
4 | 104 | 26.01.12 or 26.01.2012 |
5 | 105 | 26-01-12 or 26-10-2012 |
6 | 106 | 26 Jan 12 or 26 Jan 2012 |
7 | 107 | Jan 26, 12 or Jan 26, 2012 |
8 | 108 | 12:00:00 |
- | 9 or 109 | Jan 26 2012 12:00:00:000PM |
10 | 110 | 1-26-12 or 1-26-2012 |
11 | 111 | 12/01/26 or 2012/01/26 |
12 | 112 | 120126 or 20120126 |
- | 13 or 113 | 26 Jan 2012 12:00:00:000 |
14 | 114 | 12:00:00:000 |
- | 20 or 120 | 2012-01-26 12:00:00 |
- | 21 or 121 | 2012-01-26 12:00:00:000 |
- | 126 | 2012-01-26T12:00:00 |
- | 127 | 2012-01-26T12:00:00 |
- | 130 | 3 ???? ????? 1433 12:00:00:000PM |
- | 131 | 3/03/1433 12:00:00:000PM |
The CONVERT function is great when you want to use one of the supported date formats, but if you want something a little more customised you have to work for it.
This type of elaborate date format is possible to achieve, but you have to calculate each part of it separately.
The standard way to achieve the sorts of results shown above is to use the DATENAME function to calculate each separate part of the date and concatenate everything together with spaces in between. Easy! The DATENAME function has this basic syntax:
DATENAME(Part of date to get, Date to format)
For example, if we wanted to calculate the name of the weekday from a date we can do this:
The function is even easier to use than CONVERT.
There are many codes that can be used with the DATENAME function. the table below lists what they are using the 26th of January, 2012 at 12:30PM as an example. We've also included information on the DATEPART function in the table. This function has exactly the same syntax as the DATENAME function; the only difference is that it returns numbers rather text.
Code to use | Example of syntax | Example return value |
---|---|---|
YEAR, YYYY or YY | DATENAME(YY, AnyDate) | 2012 |
QUARTER, QQ or Q | DATENAME(QQ, AnyDate) | 1 |
MONTH, MM or M | DATENAME(MM, AnyDate) | January |
DAYOFYEAR, DY or Y | DATENAME(DY, AnyDate) | 26 |
DAY, DD or D | DATENAME(DD, AnyDate) | 26 |
WEEK, WK or WW | DATENAME(WW, AnyDate) | 4 |
WEEKDAY, DW or W | DATENAME(DW, AnyDate) | Thursday |
HOUR or HH | DATENAME(HH, AnyDate) | 12 |
MINUTE, MI or N | DATENAME(MI, AnyDate) | 30 |
SECOND, SS or S | DATENAME(SS, AnyDate) | 0 |
MILLISECOND or MS | DATENAME(MS, AnyDate) | 0 |
MICROSECOND or MCS | DATENAME(MCS, AnyDate) | 0 |
NANOSECOND or NS | DATENAME(NS, AnyDate) | 0 |
It's worth mentioning that you can't use DATENAME or DATEPART to get microseconds or nanoseconds in SQL Server 2005.
Now that you know how to calculate the different parts of a date it's relatively easy to put them together in any order to create your own custom date format. The example below generates the elaborate format shown earlier on this page:
The CASE statement is used to add the correct suffix to the day of the month.
Now that we've seen how to make dates appear in any format we choose the only thing left to learn is how to perform calculations with dates.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.