How do I convert a date to text in SQL?

Summary: in this tutorial, you will learn how to convert datetime to string in SQL Server using the

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Show
Code language: SQL (Structured Query Language) (sql)
7 function.

Using the DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2; Code language: SQL (Structured Query Language) (sql)7 function to convert datetime to string

To convert a datetime to a string, you use the

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
9 function as follows:

CONVERT(VARCHAR, datetime [,style])

Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected)

    Code language: SQL (Structured Query Language) (sql)
    0 is the first argument that represents the string type.
  • s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected)

    Code language: SQL (Structured Query Language) (sql)
    1 is an expression that evaluates to date or datetime value that you want to convert to a string
  • s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected)

    Code language: SQL (Structured Query Language) (sql)
    2 specifies the format of the date. The value of style is a number predefined by SQL Server. The

    s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected)

    Code language: SQL (Structured Query Language) (sql)
    3 parameter is optional.

The following table illustrates the valid style and the corresponding format of the datetime after converting to a string.

Without century (yy)With century (yyyy)StandardFormat–0 or 100Default for datetime and smalldatetimemon dd yyyy hh:miAM (or PM)1101U.S.1 = mm/dd/yy
101 = mm/dd/yyyy2102ANSI2 = yy.mm.dd
102 = yyyy.mm.dd3103British/French3 = dd/mm/yy
103 = dd/mm/yyyy4104German4 = dd.mm.yy
104 = dd.mm.yyyy5105Italian5 = dd-mm-yy
105 = dd-mm-yyyy6106–6 = dd mon yy
106 = dd mon yyyy7107–7 = Mon dd, yy
107 = Mon dd, yyyy8108–hh:mi:ss–9 or 109Default + millisecondsmon dd yyyy hh:mi:ss:mmmAM (or PM)10110USA10 = mm-dd-yy
110 = mm-dd-yyyy11111JAPAN11 = yy/mm/dd
111 = yyyy/mm/dd12112ISO12 = yymmdd
112 = yyyymmdd–13 or 113Europe default + millisecondsdd mon yyyy hh:mi:ss:mmm(24h)14114–hh:mi:ss:mmm(24h)–20 or 120ODBC canonicalyyyy-mm-dd hh:mi:ss(24h)–21 or 121ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffsetyyyy-mm-dd hh:mi:ss.mmm(24h)–126ISO8601yyyy-mm-ddThh:mi:ss.mmm (no spaces)–127ISO8601 with time zone Z.yyyy-mm-ddThh:mi:ss.mmmZ (no spaces)–130Hijridd mon yyyy hh:mi:ss:mmmAM–131Hijridd/mm/yyyy hh:mi:ss:mmmAM

Converting a datetime to a string examples

1) Convert datetime to string in s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)4 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)

Here is the output:

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)

2) Convert datetime to string in s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)5 and s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)6 formats example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)

Here is the output:

s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected)

Code language: SQL (Structured Query Language) (sql)

3) Convert datetime to string in s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)4 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2;

Code language: SQL (Structured Query Language) (sql)

Here is the output:

s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected)

Code language: SQL (Structured Query Language) (sql)

4) Convert datetime to string in s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)8 and s1 s2 ---------- ---------- 12/31/19 12/31/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)9 formats example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,4) s1, CONVERT(VARCHAR(10),@dt,104) s2;

Code language: SQL (Structured Query Language) (sql)

Here is the output:

s1 s2 ---------- ---------- 31.12.19 31.12.2019 (1 row affected)

Code language: SQL (Structured Query Language) (sql)

5) Convert datetime to string in DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)0 DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)1 formats example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,5) s1, CONVERT(VARCHAR(10),@dt,105) s2;

Code language: SQL (Structured Query Language) (sql)

Here is the output:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
0

6) Convert datetime to string in DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)2 DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)3 formats example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
1

Here is the output:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
2

7) Convert datetime to string in DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)4 DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)5 formats example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
3

Here is the output:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
4

8) Convert datetime to string in DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)6 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
5

Here is the output:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
6

9) Convert datetime to string in DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)7 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
7

Here is the output:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
8

10) Convert datetime to string in DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)8 and DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,3) s1, CONVERT(VARCHAR(10),@dt,103) s2; Code language: SQL (Structured Query Language) (sql)9 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(20),@dt,0) s1, CONVERT(VARCHAR(20),@dt,100) s2;

Code language: SQL (Structured Query Language) (sql)
9

Here is the result set:

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
0

11) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)0 and s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)1 format example

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
1

Here is the result set:

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
2

12) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)2 and s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)3 format example

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
3

Here is the result set:

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
4

13) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)4 format example

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
5

Here is the result set:

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
6

14) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)5 format example

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
7

Here is the result set:

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
8

15) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)6 format example

s1 s2 -------------------- -------------------- Dec 31 2019 2:43PM Dec 31 2019 2:43PM (1 row affected)

Code language: SQL (Structured Query Language) (sql)
9

Here is the result set:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
0

16) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)7 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
1

Here is the result set:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
2

17) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)8 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
3

Here is the result set:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
4

18) Convert datetime to string in s1 s2 ---------- ---------- 31/12/19 31/12/2019 (1 row affected) Code language: SQL (Structured Query Language) (sql)9 format example

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
5

Here is the result set:

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
4

In this tutorial, you have learned how to use convert a date to a string using the

DECLARE @dt DATETIME = '2019-12-31 14:43:35.863'; SELECT CONVERT(VARCHAR(10),@dt,1) s1, CONVERT(VARCHAR(10),@dt,101) s2;

Code language: SQL (Structured Query Language) (sql)
7 function.

How do I convert a date to a datatype in SQL?

SQL Server comes with the following data types for storing a date or a date/time value in the database: DATE - format YYYY-MM-DD..
DATE - format YYYY-MM-DD..
DATETIME - format: YYYY-MM-DD HH:MI:SS..
TIMESTAMP - format: YYYY-MM-DD HH:MI:SS..
YEAR - format YYYY or YY..

How to convert date to varchar in SQL?

How to get different date formats in SQL Server.
Use the SELECT statement with CONVERT function and date format option for the date values needed..
To get YYYY-MM-DD use this T-SQL syntax SELECT CONVERT(varchar, getdate(), 23).
To get MM/DD/YY use this T-SQL syntax SELECT CONVERT(varchar, getdate(), 1).

How do I convert a date to a string?

Approach:.
Get the date to be converted..
Create an instance of SimpleDateFormat class to format the string representation of the date object..
Get the date using the Calendar object..
Convert the given date into a string using format() method..
Print the result..

How do I display date in words in SQL?

SQL Date Format with the FORMAT function.
Use the FORMAT function to format the date and time data types from a date column (date, datetime, datetime2, smalldatetime, datetimeoffset, etc. ... .
To get DD/MM/YYYY use SELECT FORMAT (getdate(), 'dd/MM/yyyy ') as date..