Skip to main content

Convert DATETIME to VARCHAR in T-SQL: Explore 53 Different Format Styles with 1 Single Script

In today's post, we will learn how to convert DateTime to Text in T-SQL and demonstrate all 53 different formatting styles such as yyyy-MM-dd, yyyyMMddHHmmss with a 1 single script.

You can use this Fiddle to follow along and practice DATETIME to VARCHAR Format convertion

Convert DATETIME to VARCHAR yyyy-MM-dd

The query selects the current date and time (GETDATE()) and also converts it to the "yyyy-MM-dd" format using the style code 120.

SELECT GETDATE() AS CurrentDateTime,
CONVERT(VARCHAR(10), GETDATE(), 120) AS [yyyy-MM-dd]
CurrentDateTimeyyyy-MM-dd
2023-08-18 17:22:14.4032023-08-18

Convert DATETIME to VARCHAR yyyyMMdd

The query retrieves the current date and time (GETDATE()) and then converts it into the "yyyyMMdd" format using the style code 112. This format represents the year, month, and day in a concatenated string.

SELECT GETDATE() AS CurrentDateTime,
CONVERT(VARCHAR, GETDATE(), 112) AS yyyyMMdd
CurrentDateTimeyyyyMMdd
2023-08-18 17:22:14.40320230818

Convert DATETIME to VARCHAR yyyyMMddHHmmss

This query obtains the current date and time (GETDATE()) and then transforms it into a string without colons, dashes, or spaces. The result is formatted as "yyyyMMddHHmmss", capturing the year, month, day, hour, minute, and second components in a single sequence.

SELECT GETDATE() AS CurrentDateTime,
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR, GETDATE(), 120), ':', ''), '-', ''), ' ', '') AS yyyyMMddHHmmss
CurrentDateTimeyyyyMMddHHmmss
2023-08-18 17:22:14.40720230818172214

T-SQL to Convert DATETIME to NVARCHAR Format Styles

Dynamically generate formatted datetime values using a WHILE loop in SQL Server
DECLARE @counter INT = 0;
DECLARE @current_datetime DATETIME = GETDATE();

DROP TABLE

IF EXISTS #FormatDateDemo
CREATE TABLE #FormatDateDemo (
StyleNumber INT
,StyleOutput NVARCHAR(40)
,Query NVARCHAR(100)
);

WHILE (@counter <= 131)
BEGIN
BEGIN TRY
INSERT INTO #FormatDateDemo
SELECT @counter AS StyleNumber
,CONVERT(NVARCHAR, @current_datetime, @counter) AS StyleOutput
,CONCAT (
'SELECT CONVERT(NVARCHAR, GETDATE(), '
,CONVERT(NVARCHAR, @counter)
,')'
) AS Query;

SET @counter = @counter + 1;
END TRY

BEGIN CATCH
SET @counter = @counter + 1;
END CATCH
END

SELECT @current_datetime AS CurrentDateTime
,*
FROM #FormatDateDemo;
tip

Note that NVARCHAR is only required for style number 130.

SELECT CONVERT(NVARCHAR, GETDATE(), 130)

For other style numbers, you can simply use VARCHAR.

SELECT CONVERT(VARCHAR, GETDATE(), 23)

CurrentDateTimeStyleNumberStyleOutputQuery
2023-08-18 17:16:35.5870Aug 18 2023 5:16PMSELECT CONVERT(NVARCHAR, GETDATE(), 0)
2023-08-18 17:16:35.587108/18/23SELECT CONVERT(NVARCHAR, GETDATE(), 1)
2023-08-18 17:16:35.587223.08.18SELECT CONVERT(NVARCHAR, GETDATE(), 2)
2023-08-18 17:16:35.587318/08/23SELECT CONVERT(NVARCHAR, GETDATE(), 3)
2023-08-18 17:16:35.587418.08.23SELECT CONVERT(NVARCHAR, GETDATE(), 4)
2023-08-18 17:16:35.587518-08-23SELECT CONVERT(NVARCHAR, GETDATE(), 5)
2023-08-18 17:16:35.587618 Aug 23SELECT CONVERT(NVARCHAR, GETDATE(), 6)
2023-08-18 17:16:35.5877Aug 18, 23SELECT CONVERT(NVARCHAR, GETDATE(), 7)
2023-08-18 17:16:35.587817:16:35SELECT CONVERT(NVARCHAR, GETDATE(), 8)
2023-08-18 17:16:35.5879Aug 18 2023 5:16:35:587PMSELECT CONVERT(NVARCHAR, GETDATE(), 9)
2023-08-18 17:16:35.5871008-18-23SELECT CONVERT(NVARCHAR, GETDATE(), 10)
2023-08-18 17:16:35.5871123/08/18SELECT CONVERT(NVARCHAR, GETDATE(), 11)
2023-08-18 17:16:35.58712230818SELECT CONVERT(NVARCHAR, GETDATE(), 12)
2023-08-18 17:16:35.5871318 Aug 2023 17:16:35:587SELECT CONVERT(NVARCHAR, GETDATE(), 13)
2023-08-18 17:16:35.5871417:16:35:587SELECT CONVERT(NVARCHAR, GETDATE(), 14)
2023-08-18 17:16:35.587202023-08-18 17:16:35SELECT CONVERT(NVARCHAR, GETDATE(), 20)
2023-08-18 17:16:35.587212023-08-18 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 21)
2023-08-18 17:16:35.5872208/18/23 5:16:35 PMSELECT CONVERT(NVARCHAR, GETDATE(), 22)
2023-08-18 17:16:35.587232023-08-18SELECT CONVERT(NVARCHAR, GETDATE(), 23)
2023-08-18 17:16:35.5872417:16:35SELECT CONVERT(NVARCHAR, GETDATE(), 24)
2023-08-18 17:16:35.587252023-08-18 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 25)
2023-08-18 17:16:35.587262023-18-08 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 26)
2023-08-18 17:16:35.5872708-18-2023 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 27)
2023-08-18 17:16:35.5872808-2023-18 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 28)
2023-08-18 17:16:35.5872918-08-2023 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 29)
2023-08-18 17:16:35.5873018-2023-08 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 30)
2023-08-18 17:16:35.587312023-18-08SELECT CONVERT(NVARCHAR, GETDATE(), 31)
2023-08-18 17:16:35.5873208-18-2023SELECT CONVERT(NVARCHAR, GETDATE(), 32)
2023-08-18 17:16:35.5873308-2023-18SELECT CONVERT(NVARCHAR, GETDATE(), 33)
2023-08-18 17:16:35.5873418-08-2023SELECT CONVERT(NVARCHAR, GETDATE(), 34)
2023-08-18 17:16:35.5873518-2023-08SELECT CONVERT(NVARCHAR, GETDATE(), 35)
2023-08-18 17:16:35.587100Aug 18 2023 5:16PMSELECT CONVERT(NVARCHAR, GETDATE(), 100)
2023-08-18 17:16:35.58710108/18/2023SELECT CONVERT(NVARCHAR, GETDATE(), 101)
2023-08-18 17:16:35.5871022023.08.18SELECT CONVERT(NVARCHAR, GETDATE(), 102)
2023-08-18 17:16:35.58710318/08/2023SELECT CONVERT(NVARCHAR, GETDATE(), 103)
2023-08-18 17:16:35.58710418.08.2023SELECT CONVERT(NVARCHAR, GETDATE(), 104)
2023-08-18 17:16:35.58710518-08-2023SELECT CONVERT(NVARCHAR, GETDATE(), 105)
2023-08-18 17:16:35.58710618 Aug 2023SELECT CONVERT(NVARCHAR, GETDATE(), 106)
2023-08-18 17:16:35.587107Aug 18, 2023SELECT CONVERT(NVARCHAR, GETDATE(), 107)
2023-08-18 17:16:35.58710817:16:35SELECT CONVERT(NVARCHAR, GETDATE(), 108)
2023-08-18 17:16:35.587109Aug 18 2023 5:16:35:587PMSELECT CONVERT(NVARCHAR, GETDATE(), 109)
2023-08-18 17:16:35.58711008-18-2023SELECT CONVERT(NVARCHAR, GETDATE(), 110)
2023-08-18 17:16:35.5871112023/08/18SELECT CONVERT(NVARCHAR, GETDATE(), 111)
2023-08-18 17:16:35.58711220230818SELECT CONVERT(NVARCHAR, GETDATE(), 112)
2023-08-18 17:16:35.58711318 Aug 2023 17:16:35:587SELECT CONVERT(NVARCHAR, GETDATE(), 113)
2023-08-18 17:16:35.58711417:16:35:587SELECT CONVERT(NVARCHAR, GETDATE(), 114)
2023-08-18 17:16:35.5871202023-08-18 17:16:35SELECT CONVERT(NVARCHAR, GETDATE(), 120)
2023-08-18 17:16:35.5871212023-08-18 17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 121)
2023-08-18 17:16:35.5871262023-08-18T17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 126)
2023-08-18 17:16:35.5871272023-08-18T17:16:35.587SELECT CONVERT(NVARCHAR, GETDATE(), 127)
2023-08-18 17:16:35.587130 2 صفر 1445 5:16:35:587PMSELECT CONVERT(NVARCHAR, GETDATE(), 130)
2023-08-18 17:16:35.587131 2/02/1445 5:16:35:587PMSELECT CONVERT(NVARCHAR, GETDATE(), 131)

Script Explanation

Let's walk through the SQL script step by step:

  1. Variable Declarations:

    • @counter INT = 0;: Initializes a counter variable to track the formatting style numbers.
    • @date DATETIME = GETDATE();: Retrieves the current date and time and assigns it to the @date variable.
  2. Temporary Table Creation and Dropping:

    • DROP TABLE IF EXISTS #FormatDateDemo: If the temporary table #FormatDateDemo already exists, it is dropped. This ensures that any previous version of the table is removed before creating a new one.
    • CREATE TABLE #FormatDateDemo ...: Creates a temporary table named #FormatDateDemo with three columns: StyleNumber, StyleOutput, and Query.
  3. While Loop:

    • WHILE (@counter <= 131): Initiates a loop that continues as long as the @counter value is less than or equal to 131.
  4. Loop Body:

    • BEGIN TRY: Starts a try block for error handling within the loop.

      • INSERT INTO #FormatDateDemo ...: Inserts a row into the #FormatDateDemo table with the following values:

        • StyleNumber: The current value of the @counter variable.
        • StyleOutput: The result of converting the @date using the current @counter as the formatting style.
        • Query: A string that represents the SQL query to get the same formatted datetime value.
      • SET @counter = @counter + 1;: Increments the @counter variable by 1.

    • BEGIN CATCH: Catches any errors that might occur during the try block.

      • SET @counter = @counter + 1;: Increments the @counter variable even if an error occurs, ensuring the loop continues to the next iteration.
  5. Temporary Table Query:

    • SELECT * FROM #FormatDateDemo;: Retrieves all rows from the #FormatDateDemo temporary table.

In summary, this script generates formatted datetime values using different styles (0 to 131) and stores the style number, formatted output, and corresponding query in a temporary table. The loop iterates through the styles, attempting to insert formatted datetime values into the temporary table. Any errors are caught, and the loop continues to the next iteration. Finally, the script retrieves the results from the temporary table. This script can be useful for understanding how different formatting styles affect datetime values in SQL Server and for generating sample queries.