Error while converting nvarchar to datetime
I would like to execute my MSSQL stored procedure from my java program by giving as parameters two dates to have aselection between this two dates. So in java code I proceeded like this:
"EXEC [dbo].[MyProcedure] " + "'"+ dateFrom+"'" ,"'"+dateTo"'"
I have declared my parameters in sql server procedure like this way :
@collected_date1 datetime, @collected_date2 datetime.
And I selected the two date like this :
SELECT @date1 = Convert(varchar(23),@collected_date1,121) SELECT @date2= Convert(varchar(23),@collected_date2, 121)
However I have this message error :
Error converting data type nvarchar to datetime
Could you help me please ?
One Solution collect form web for “Error while converting nvarchar to datetime”
on above statement you declared variable
and when you set you convert data-type to varchar so it give you error
if you want to set into datetime datatype you should use
SELECT @date2= Convert(datetime,@collected_date2, 121) ;