Str_to_date function in sql server?

MySQL has a function STR_TO_DATE, which converts a string to a date .

Question:

Is there a similar feature in SQL Server?

+4
source share
6 answers

If you need to parse a specific format, use CONVERT(datetime, @mystring, @format) . Use this as a link: http://www.sqlusa.com/bestpractices/datetimeconversion/

+5
source

What if the line is 7/7/2010?

Then use CONVERT with 101 (mm / dd / yy) or 103 (dd / mm / yy) depending on what you want:

 SELECT CONVERT(DATE, '7/7/2010', 103) 

Result:

 2010-07-07 
+5
source

Use CAST .

 declare @MyString varchar(10) declare @MyDate datetime set @MyString = '2010-08-19' set @MyDate = cast(@MyString as datetime) select @MyDate 
+2
source
 CAST(<string> AS DATETIME) 
+1
source

Here is a good example:

 declare @myDate datetime set @myDate = '06/09/2017' select concat(convert(varchar(20), @myDate,101), ' -- ', convert(varchar(20), @myDate,103), ' -- ', convert(varchar(20), @myDate,6)) 

This is what you get, depending on 101 or 103 or 6 :

09/06/2017 -- 06/09/2017 -- 06 Sep 17

A good summary of date types here - https://www.w3schools.com/sql/func_convert.asp

+1
source

In MSSQL: select cast ('2012/06/12 10:32 AM' as date-time);

You will receive: 2012-06-12 10: 32: 00.000

-1
source

All Articles