I have a big procedure which consists mostly dynamic SQL. I am having issues with setting one of the date fields.
DECLARE @WorkDate DATETIME
SET @WorkDate = 'SELECT MIN(__Insert_Date) FROM ' + @DatabaseName + '.'
+ @SchemaName + '.' + @TableName + '_Hist'
SET @WorkDate = DATEADD(DAY, DATEDIFF(DAY, '19000101', @WorkDate), '19000101')
This is part of a big procedure. So when I execute the above query I am getting this error:
Msg 241, Level 16, State 1, Line 68
Conversion failed when converting date and/or time from character string.
@WorkDate is DATETIME and your setting it at as a sting thats why the conversion is failing
EDIT :
Try this:
If you need to insert the
@WorkDateinto the select string where__Insert_Dateis then you need to reverse the order to look like thisDon’t know if you looking to insert the
@WorkDateinto the select string but that’s how you could accomplish that