Summary: in this tutorial, you will learn how to convert datetime to string in SQL Server using the 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 stringTo convert a datetime to a string, you use the 9 function as follows:
In this syntax:
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/yy101 = 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 examples1) 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
Here is the output:
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
Here is the output:
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
Here is the output:
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
Here is the output:
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
Here is the output: 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 example1 Here is the output: 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 example3 Here is the output: 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 example5 Here is the output: 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 example7 Here is the output: 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 example9 Here is the result set: 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 example1 Here is the result set: 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 example3 Here is the result set: 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 example5 Here is the result set: 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 example7 Here is the result set: 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 example9 Here is the result set: 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 example1 Here is the result set: 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 example3 Here is the result set: 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 example5 Here is the result set: 4 In this tutorial, you have learned how to use convert a date to a string using the 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.. |