I have this query to fill in a date in a table that's giving me this error message:
Conversion failed when converting datetime from character string.
Below are my table declaration and query. What am I doing wrong and how can I fix it?
CREATE TABLE #inv
(
Rep_LName NVARCHAR(50)
, Rep_FName NVARCHAR(50)
, Rep_ID NVARCHAR(50)
, Rep_Email NVARCHAR(100)
, Rep_Status NVARCHAR(50)
, Rep_BU NVARCHAR(50)
, Sales_Force NVARCHAR(50)
, Territory NVARCHAR(50)
, Sample_Eligibility NVARCHAR(50)
, DM_Name NVARCHAR(100)
, Phys_Inv_Date DATETIME
, Last_Reconciled DATETIME
, Inv_Type NVARCHAR(50)
, Days_Since_Last_inv INT
)
I'm trying to fill the Phys_Inv_Date field inside a cursor like so:
OPEN Inventory_info
FETCH NEXT FROM Inventory_info INTO @rep_ID, @call_date
WHILE ( @@fetch_status = 0 ) BEGIN
SELECT
@ls_Sql = 'update #inv set Phys_Inv_Date = case when inventory_type = ''physical'' then '
+ @call_date
+ ' else b.inv_date end from #inv a INNER JOIN (select top 1 call_date, rep_id from inv_header where call_date < '
+ @call_date + ' and rep_id = ''' + @rep_id
+ ''') b ON a.rep_id = b.rep_id WHERE Phys_Inv_Date IS NULL'
EXEC (@ls_Sql)
FETCH NEXT FROM Inventory_info INTO @rep_ID, @call_date
END
CLOSE Inventory_info
DEALLOCATE Inventory_info
datetimevalues.