CONVERT() from Date/Time to String Examples in SQL Server

The CONVERT() function allows you to convert between data types. It’s similar to the CAST() function, but one of the benefits of CONVERT() is that, when you convert from a date/time data type to a string, you can add an optional argument that specifies the style that you want the return value to be in. For example, you can have it returned as dd.mm.yyyy, yyyy-mm-dd, dd mon yyyy, etc

This article contains examples of the various styles you can return when converting a date/time value to a string using the CONVERT() function in SQL Server.

Basic Example

The default style when converting from the datetime and smalldatetime data types is 0 and 100 (these represent the same style). Therefore, when you don’t provide a style (third parameter), this is how it’s styled:

DECLARE @date datetime = GETDATE();
SELECT
    @date AS Original,
    CONVERT(varchar, @date) AS Converted;

Result:

+-------------------------+---------------------+
| Original                | Converted           |
|-------------------------+---------------------|
| 2018-06-07 03:08:21.997 | Jun  7 2018  3:08AM |
+-------------------------+---------------------+

However, you’ll get a different result if the original data type is not datetime or smalldatetime.

If you need it to be returned in a different style, you’ll need to specify a third argument.

Styles with Two Digit Years

Below are examples of the various values you can use to specify the style using a two digit year component.

Styles 1 to 6

DECLARE @date datetime2 = '2018-06-07';
SELECT
    CONVERT(nvarchar(30), @date, 1) AS '1',
    CONVERT(nvarchar(30), @date, 2) AS '2',
    CONVERT(nvarchar(30), @date, 3) AS '3',
    CONVERT(nvarchar(30), @date, 4) AS '4',
    CONVERT(nvarchar(30), @date, 5) AS '5',
    CONVERT(nvarchar(30), @date, 6) AS '6';

Result:

+----------+----------+----------+----------+----------+-----------+
| 1        | 2        | 3        | 4        | 5        | 6         |
|----------+----------+----------+----------+----------+-----------|
| 06/07/18 | 18.06.07 | 07/06/18 | 07.06.18 | 07-06-18 | 07 Jun 18 |
+----------+----------+----------+----------+----------+-----------+

Styles 7 to 6

DECLARE @date datetime2 = '2018-06-07';
SELECT
    CONVERT(nvarchar(30), @date, 7) AS '7',
    CONVERT(nvarchar(30), @date, 8) AS '8',
    CONVERT(nvarchar(30), @date, 10) AS '10',
    CONVERT(nvarchar(30), @date, 11) AS '11',
    CONVERT(nvarchar(30), @date, 12) AS '12',
    CONVERT(nvarchar(30), @date, 14) AS '14';

Result:

+------------+----------+----------+----------+--------+------------------+
| 7          | 8        | 10       | 11       | 12     | 14               |
|------------+----------+----------+----------+--------+------------------|
| Jun 07, 18 | 00:00:00 | 06-07-18 | 18/06/07 | 180607 | 00:00:00.0000000 |
+------------+----------+----------+----------+--------+------------------+

Styles with Four Digit Years

Below are examples of the various values you can use to specify the style using a four digit year component.

Styles 100 to 103

DECLARE @date datetime2 = '2018-06-07 02:35:52.8537677';
SELECT
    CONVERT(nvarchar(30), @date, 100) AS '100',
    CONVERT(nvarchar(30), @date, 101) AS '101',
    CONVERT(nvarchar(30), @date, 102) AS '102',
    CONVERT(nvarchar(30), @date, 103) AS '103';

Result:

+---------------------+------------+------------+------------+
| 100                 | 101        | 102        | 103        |
|---------------------+------------+------------+------------|
| Jun  7 2018  2:35AM | 06/07/2018 | 2018.06.07 | 07/06/2018 |
+---------------------+------------+------------+------------+

Styles 104 to 108

DECLARE @date datetime2 = '2018-06-07 02:35:52.8537677';
SELECT
    CONVERT(nvarchar(30), @date, 104) AS '104',
    CONVERT(nvarchar(30), @date, 105) AS '105',
    CONVERT(nvarchar(30), @date, 106) AS '106',
    CONVERT(nvarchar(30), @date, 107) AS '107',
    CONVERT(nvarchar(30), @date, 108) AS '108';

Result:

+------------+------------+-------------+--------------+----------+
| 104        | 105        | 106         | 107          | 108      |
|------------+------------+-------------+--------------+----------|
| 07.06.2018 | 07-06-2018 | 07 Jun 2018 | Jun 07, 2018 | 02:35:52 |
+------------+------------+-------------+--------------+----------+

Styles 109 to 112

DECLARE @date datetime2 = '2018-06-07 02:35:52.8537677';
SELECT
    CONVERT(nvarchar(30), @date, 109) AS '109',
    CONVERT(nvarchar(30), @date, 110) AS '110',
    CONVERT(nvarchar(30), @date, 111) AS '111',
    CONVERT(nvarchar(30), @date, 112) AS '112';

Result:

+--------------------------------+------------+------------+----------+
| 109                            | 110        | 111        | 112      |
|--------------------------------+------------+------------+----------|
| Jun  7 2018  2:35:52.8537677AM | 06-07-2018 | 2018/06/07 | 20180607 |
+--------------------------------+------------+------------+----------+

Styles 113 to 114

DECLARE @date datetime2 = '2018-06-07 02:35:52.8537677';
SELECT
    CONVERT(nvarchar(30), @date, 113) AS '113',
    CONVERT(nvarchar(30), @date, 114) AS '114';

Result:

+------------------------------+------------------+
| 113                          | 114              |
|------------------------------+------------------|
| 07 Jun 2018 02:35:52.8537677 | 02:35:52.8537677 |
+------------------------------+------------------+

Styles 120 to 127

DECLARE @date datetime2 = '2018-06-07';
SELECT    
    CONVERT(nvarchar(30), @date, 120) AS '120',
    CONVERT(nvarchar(30), @date, 126) AS '126',
    CONVERT(nvarchar(30), @date, 127) AS '127';

Result:

+---------------------+---------------------+---------------------+
| 120                 | 126                 | 127                 |
|---------------------+---------------------+---------------------|
| 2018-06-07 00:00:00 | 2018-06-07T00:00:00 | 2018-06-07T00:00:00 |
+---------------------+---------------------+---------------------+

Style 130

DECLARE @date datetime2 = '2018-06-07';
SELECT    
    CONVERT(nvarchar(30), @date, 130) AS '130';

Result:

+--------------------------------+
| 130                            |
|--------------------------------|
| 24 رمضان 1439 12:00:00.0000000 |
+--------------------------------+

Microsoft warns that this value does not render correctly on a default US installation of SSMS.

Style 131

DECLARE @date datetime2 = '2018-06-07';
SELECT    
    CONVERT(nvarchar(30), @date, 131) AS '131';

Result:

+-------------------------------+
| 131                           |
|-------------------------------|
| 24/09/1439 12:00:00.0000000AM |
+-------------------------------+

You can read more about the available date/time styles on the Microsoft website.