views:

45

answers:

4

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

Question:

Is there a similar function in SQL Server?

+1  A: 
CAST(<string> AS DATETIME)
Tom H.
can u give an example of how to use it? what is the string is 7/7/2010 as opposed to 07/07/10 ??
I__
CAST/CONVERT works if you have a supported format - [STR_TO_DATE supports custom formats](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date).
OMG Ponies
+4  A: 

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

Aaron D
+2  A: 

Use CAST.

declare @MyString varchar(10)
declare @MyDate datetime

set @MyString = '2010-08-19'
set @MyDate = cast(@MyString as datetime)
select @MyDate
Joe Stefanelli
+2  A: 

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
Mark Byers