convert string to date

Hemant Karnik 121 Reputation points
2022-01-06T12:22:01.397+00:00

hi
I have a date in below format
'2020-08-17T16:28:46' as string

want to convert in date

please help
i am trying as below

convert(DATETIME,format(insta_map_date,'dd-MMM-yyyy'),103))

the error is Argument data type varchar is invalid for argument 1 of format function. Severity 16

please help

but with error
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-01-06T13:58:36.36+00:00

    It's a simple as this:

    SELECT convert(datetime, '2020-08-17T16:28:46')
    

    This is one of the safe formats, which will always be interpreted the one and the same, no matter the setting for DATEFORMAT.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2022-01-06T12:41:51.6+00:00

    convert(DATETIME,format(insta_map_date,'dd-MMM-yyyy'), 103 ))

    Dateformat parameter 103 = British is wrong, use 126 = ISO8601 instead

    select convert(datetime, '2020-08-17T16:28:46', 126)  
    

    See CONVERT => Date and Time styles

    0 comments No comments

  2. Sreeju Nair 12,661 Reputation points
    2022-01-06T13:11:33.063+00:00

    Based on your question, I believe insta_map_date is a varchar field. and based on the source data sample you provided, the date format matches with ISO8601. So you can use 126 or 127 as the style while converting to datetime.

    Try the following.

    convert(DATETIME,insta_map_date, 126)

    For more information about SQL Server Date Formats, refer : https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-ver15

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.