Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server convert string to datetime

This is not asking how to convert an arbitrary string to datetime in MSSQL such as this question.

I can control the string format but I want to know what the MSSQL syntax is for updating a datetime field using a date string.

like image 506
NeilG Avatar asked Jul 16 '09 06:07

NeilG


People also ask

How do I convert text to date in SQL?

SQL Server: Convert string to date explicitly In SQL Server, converting a string to date explicitly can be achieved using CONVERT(). CAST() and PARSE() functions.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.


1 Answers

UPDATE MyTable SET MyDate = CONVERT(datetime, '2009/07/16 08:28:01', 120) 

For a full discussion of CAST and CONVERT, including the different date formatting options, see the MSDN Library Link below:

https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql

like image 91
Bork Blatt Avatar answered Sep 26 '22 00:09

Bork Blatt