The below dynamic SQL throws an error:
The conversion failed when converting character string to smalldatetime data type
My code:
DECLARE @pTimeStamp smalldatetime
SET @pTimeStamp = '2017-05-22 12:15:00'
DECLARE @SQLQuery AS NVARCHAR(4000)
Set @SQLQuery = N'Select *' +
' From SampleTable' +
' Where TimeStamp = ' + @pTimeStamp
EXECUTE sp_executesql @SQLQuery
I've also tried
Convert(smalldatetime, @pTimeStamp, 20)
as well as
CAST(@pTimeStamp AS smalldatetime)
but I only get other errors. I also tried declaring @pTimeStamp as varchar(50) and then converting but still got errors.
Doing something simple like:
DECLARE @pTimeStamp smalldatetime
SET @pTimeStamp = '2012-01-22 12:15:00'
Select *
From SampleTable
Where TimeStamp = @pTimeStamp
ran fine so I'm guessing it has to do with the dynamic SQL.
Please help....