I'm working on a stored procedure in Microsoft SQL Server that currently gives the error:
Conversion failed when converting datetime from character string.
Here is a code snippet, but let me first explain what is going on. The @Query parameter is going to be run later on as part of a call to sp_send_dbmail.
SET @Query = 'SELECT * FROM dbo.Test WITH (NOLOCK) WHERE Test.TestID = 1 AND Test.Date > ''' + CAST(@Today AS nvarchar(20)) + ''''
There are multiple queries but the first thing I wanted to rule out is that casting nvarchar(20) should be sufficient. Its possible that when it cast the date as a string, when it tried to execute the query later it wasn't enough to convert to a date time. Is casting from a datetime to a length 20 nvarchar enough?
Conversion failed when converting datetime from character string.
Here is a code snippet, but let me first explain what is going on. The @Query parameter is going to be run later on as part of a call to sp_send_dbmail.
SET @Query = 'SELECT * FROM dbo.Test WITH (NOLOCK) WHERE Test.TestID = 1 AND Test.Date > ''' + CAST(@Today AS nvarchar(20)) + ''''
There are multiple queries but the first thing I wanted to rule out is that casting nvarchar(20) should be sufficient. Its possible that when it cast the date as a string, when it tried to execute the query later it wasn't enough to convert to a date time. Is casting from a datetime to a length 20 nvarchar enough?