T-SQL Tutorial

How to convert Varchar to DateTime


To convert a Varchar to DateTime uses sql conversion functions like try_parse or convert.

Syntax

TRY_PARSE ( string_value AS data_type [ USING culture ] )

CONVERT ( datatype [ ( length ) ] , expression [ , style ] )





Examples

SELECT Convert(VARCHAR(15),'08/16/2019',101);
SELECT Convert(DATETIME, '16/08/2019 09:31:44', 103);

SELECT TRY_PARSE('08/16/2019 09:27:37 AM' AS DATETIME USING 'en-us');

DECLARE @varchar_date varchar(50);
SET @varchar_date = '2019-08-16 09:37:00'
SELECT CONVERT(datetime, @varchar_date)
PRINT @varchar_date;