MySQL has a function called STR_TO_DATE, that converts a string to date.
Question:
Is there a similar function in SQL Server?
MySQL has a function called STR_TO_DATE, that converts a string to date.
Is there a similar function in SQL Server?
If you need to parse a particular format, use CONVERT(datetime, @mystring, @format)
. Use this as a reference: http://www.sqlusa.com/bestpractices/datetimeconversion/
Use CAST.
declare @MyString varchar(10)
declare @MyDate datetime
set @MyString = '2010-08-19'
set @MyDate = cast(@MyString as datetime)
select @MyDate
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