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 :

  • SQL Server NText field limited to 43,679 characters?
  • How to get the stored procedure result using perl?
  • Cannot insert NULL values into column 'USERNAME', table 'tempdb.dbo.#temptable error
  • Sql get name and parent in while loop
  • Tell SQL Server 2008 R2 to use user name a password to avoid error 18452 using Access 2010 VBA and linked tables
  • Error connecting to db mssql php zend 1.12
  • 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 ?
    Thank you

  • Is it necessary to use # for creating temp tables in SQL server?
  • Use SQL Server to get all the data from XML nodes named the same
  • Pyodbc Accessing Multiple Databases on same server
  • How to get first and last day of previous month (with timestamp) in SQL Server
  • Storing and comparing the date and time from SQL Server to .NET when application is used in different time zones
  • Joining of two tables in SQL Server
  • One Solution collect form web for “Error while converting nvarchar to datetime”

    on above statement you declared variable

    @collected_date1 datetime,
    @collected_date2 datetime

    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)  ;
    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.