I need some assistance with finding where the error in my procedure code it. This same code worked yesterday and now it is not. I am getting the error message Msg 241, Level 16, State 1, Procedure Peoplesoft.GLStatisticsFactGold_Insert, Line 140 Conversion failed when converting date and/or time from character string. The column types did not change. Here is the original code :
DECLARE @maxdate date
-- DECLARE @mindate date
-- ;With dd as
-- (Select
-- *
-- From dbo.DateDim
-- where Cast(DateValue as date) = cast(GETDATE() as date)
-- )
-- SELECT
-- @maxdate = [maxmonth]
-- ,@mindate = [minMonth]
-- from
-- (
-- SELECT
-- cast(FiscalMonthNumber as varchar(max)) + '/01/' + Cast(FiscalYear as varchar(max)) [minMonth],
-- (Select cast(FiscalMonthNumber as varchar(max)) + '/01/' + Cast(FiscalYear as varchar(max))FRom dd) [maxmonth]
-- from dbo.DateDim
-- where
-- CAST(DateValue as date) = (Select dateadd(MONTH,-3,DateValue) from dd)
-- ) a
-- DELETE FROM FPG_GOLD_WH.Peoplesoft.GLStatisticsFact where FiscalDate Between @mindate and @maxdate
-- INSERT INTO FPG_GOLD_WH.Peoplesoft.GLStatisticsFact
-- (
-- [EntityCode]
-- ,[DepartmentCode]
-- ,[AccountCode]
-- ,[FiscalDate]
-- ,[TimeClass]
-- ,[Amount]
-- ,[Affiliate]
-- ,[CreatedDate]
-- ,[LastUpdatedDate]
-- )
-- SELECT
-- [EntityCode]
-- ,[DepartmentCode]
-- ,[AccountCode]
-- ,[FiscalDate]
-- ,[TimeClass]
-- ,[Amount]
-- ,[Affiliate]
-- ,[CreatedDate]
-- ,[LastUpdatedDate]
-- FROM FPG_SILVER_WH.Peoplesoft.GLStatisticsFact
But I needed to update it to because it was deleting data. What I needed to procedure to do is look at the silver table and if the same fiscal date is in the gold table then delete it from gold and replace it with the rows in silver, if the data is in silver and not in gold then add it to gold.
I updated the code with this. This code worked yesterday but today it is not.
DECLARE @maxdate date
DECLARE @mindate date
;WITH dd AS
(SELECT * FROM dbo.DateDim WHERE CAST(DateValue AS date) = CAST(GETDATE() AS date))
SELECT
@maxdate = [maxmonth],
@mindate = [minMonth]
FROM
(
SELECT
CAST(FiscalMonthNumber AS varchar(max)) + '/01/' + CAST(FiscalYear AS varchar(max)) AS [minMonth],
(SELECT CAST(FiscalMonthNumber AS varchar(max)) + '/01/' + CAST(FiscalYear AS varchar(max)) FROM dd) AS [maxmonth]
FROM dbo.DateDim
WHERE
CAST(DateValue AS date) = (SELECT DATEADD(MONTH, -3, DateValue) FROM dd)
) a
-- Check if fiscal months exist in silver_WH before deleting
IF EXISTS (SELECT 1 FROM FPG_SILVER_WH.Peoplesoft.GLStatisticsFact WHERE FiscalDate BETWEEN @mindate AND @maxdate)
BEGIN
DELETE FROM FPG_GOLD_WH.Peoplesoft.GLStatisticsFact WHERE FiscalDate BETWEEN @mindate AND @maxdate
INSERT INTO FPG_GOLD_WH.Peoplesoft.GLStatisticsFact
(
[EntityCode]
,[DepartmentCode]
,[AccountCode]
,[FiscalDate]
,[TimeClass]
,[Amount]
,[Affiliate]
,[CreatedDate]
,[LastUpdatedDate]
)
SELECT
[EntityCode]
,[DepartmentCode]
,[AccountCode]
,[FiscalDate]
,[TimeClass]
,[Amount]
,[Affiliate]
,[CreatedDate]
,[LastUpdatedDate]
FROM FPG_SILVER_WH.Peoplesoft.GLStatisticsFact
END
Any help would be greatly appreciated.