Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str_to_date function in sql server?

MySQL has a function called STR_TO_DATE, that converts a string to date.

Question:

Is there a similar function in SQL Server?

like image 862
Alex Gordon Avatar asked Aug 19 '10 19:08

Alex Gordon


2 Answers

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

like image 139
Aaron D Avatar answered Oct 06 '22 12:10

Aaron D


What if the string is 7/7/2010?

Then use CONVERT with either 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
like image 22
Mark Byers Avatar answered Oct 06 '22 10:10

Mark Byers