So I have a base table that looks something like this.
SELECT [BILL_MONTH]
,[BILL_YEAR]
,[BILLED]
FROM bill_Detail
Everything is varchar.
I created a view based on that table that converts the bill year and bill month to a datetime field. Of course, the bill_month and bill_year fields have some crap data that doesn't convert so I have the following WHERE clause in my view definition.
WHERE ISDATE(CONVERT(varchar(4), BILL_YEAR) + '-' + CONVERT(varchar(3), BILL_MONTH) + '-1' ) = 1
The view works like a champ when I do a plain select on it. No bad dates, everything cleaned up, etc. However, when I try and do any sort of date arithmetic (dateadd, datediff) I start getting conversion errors.
EDIT: Adding in actual view and test select statement:
Code for the view
SELECT ID
,BILLED
,Payment_Type
,CONVERT(datetime, CONVERT(varchar(4), BILL_YEAR) + '-' + CONVERT(varchar(3), BILL_MONTH) + '-1' )
AS BillDate FROM dbo.Detail AS d WHERE ISDATE(CONVERT(varchar(4), BILL_YEAR) + '-' + CONVERT(varchar(3), BILL_MONTH) + '-1' ) = 1 AND ISNULL(Payment_Code,'') = '' AND Payment_Type in ('D','I')
Here is the select statement that blows up.
SELECT ID
,[BILLED]
,[Payment_Type]
,[BillDate] FROM vw_DelinquentDetail where isdate(billdate) = 1
The select statement is where the following error pops up.
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
Sorry for the code formatting on the first section.