Saranya Rajendran

Saranya Rajendran

  • NA
  • 53
  • 13.3k

varchar to datetime conversion in sql 2008

Feb 2 2016 5:42 AM
I need to convert varchar to datetime..I've tried something like following

declare @date varchar(150)='01/10/15'
declare @datenew date
select @datenew=convert(smalldatetime,CONVERT(nvarchar(10), CAST(@date AS DATETIME), 101) ,101)
print @datenew

the output is : 2015-01-10

But when I change the date to

declare @date varchar(150)='13/10/15'

it throws the following error.. :
"The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."

the resultant format of datetime is YYYY-MM-DD. I want the result in the format DD-MM-YYYY..
What do i do..

Thanks in advance

Answers (2)