I am trying to dynamically get the last day of the prior month and pass it into a query. I tried the following idea, but it dind't work.
DECLARE @PrevMonthLastDay DATE=(SELECT CONVERT(CHAR(15),DATEADD(DD,-Day(GETDATE()),GETDATE()),106))
--PRINT @PrevMonthLastDay
INSERT INTO [TBL_ParseRawDataHist]
SELECT [SrcID],[ASOFDATE],
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 1) AS Parse1,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 2) AS Parse2,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 3) AS Parse3,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 4) AS Parse4,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 5) AS Parse5,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 6) AS Parse6,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 7) AS Parse7,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 8) AS Parse8,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 9) AS Parse9,
dbo.SplitIndex('|', LTRIM(RTRIM([SrcID])), 10) AS Parse10
FROM TBL_FR2052A_RAW_DATA_HIST
--WHERE ASOFDATE = '02/28/2018'
WHERE ASOFDATE = ''' + CAST(@PrevMonthLastDay AS varchar(15)) + '''
Result: Conversion failed when converting date and/or time from character string.
What am I doing wrong here? I am using SQL Server 2008!