Reputation: 107
I have a table with a month name in it as type varchar. e.g. "November". Can I convert this to a date field?
CONVERT(DATETIME,main.ReportMonth) AS ReportMonthDate
CAST(main.ReportMonth AS DATETIME) AS ReportMonthDate
both result in a conversion failure.
I'm using SQL Server 2008.
Upvotes: 0
Views: 3029
Reputation: 33571
You can hard code a string value at the end of your input value. Something like this.
declare @ReportMonth varchar(10) = 'November'
select cast(@ReportMonth + ' 1, 2015' as date)
Or if you want to make the year portion be dynamic based on the current date you could modify that slightly like this.
select cast(@ReportMonth + ' 1, ' + cast(datepart(year, getdate()) as char(4)) as date)
Upvotes: 1