I have searched about 20 results apertaining to the error message “Conversion failed when converting date and/or time from character string.” and none seem to address my issue.
A little history, last Friday the queries were working just fine and the data returned was proper. On the following Monday, the queries no longer worked and gave this error message, “Conversion failed when converting date and/or time from character string.” So, after a few hours of battling it was able to run a function with the offending code in it and the query ran perfect again. Until this morning. I came into the office and ran the query and received the error “Conversion failed when converting date and/or time from character string.” So now I am pulling my hair out.
I started by verifying each record being returned is, in fact, a valid date using the isDate function. All the dates are valid dates in string format. I am simply doing a check to see whether or not the record is older than 1 year.
I do have several INNER JOINS and have been wondering if they are affecting the output.
SELECT
gc2p.partnumber, gc2p.orderby, gc2p.campaigncode, gp2a.assetfilename
FROM [StepMirror].[dbo].[stepview_nwppck_ngn_getclassification2productrefs] gc2p
,[StepMirror].[dbo].[stepview_nwppck_ngn_getpimweblegalattrlist1] gpwa
,[StepMirror].[dbo].[stepview_nwppck_ngn_getclassification2assetrefs] gc2a
,[StepMirror].[dbo].[stepview_nwppck_ngn_getproduct2assetrefs] gp2a
WHERE gc2p.partnumber=gpwa.PARTNUMBER and
gc2p.id=gc2a.id and
gp2a.PRODUCTNAME=gpwa.PARTNUMBER and
ATTRIBUTENAME='New Date' AND
ATTRIBUTEVALUE > dateadd(month,-12,getdate()) AND
gc2p.id = '5665976' and
gc2a.assettype='Primary Image' AND
gp2a.ASSETTYPE = 'Primary Image'
order by gc2p.orderby
If anyone could give me a helping hand that would be wonderful.
Edit: The query runs fine when I remove ‘ATTRIBUTEVALUE > dateadd(month,-12,getdate())’. I forgot to mention the exact part of the query throwing the error.
Edit: Updated Query – Now Working Query for those seeking a similar answer.
SELECT TOP 18 gc2p.partnumber, gpwa.ATTRIBUTECNAME,gp2a.ASSETFILENAME, gpwa.ATTRIBUTEVALUE
FROM [StepMirror].[dbo].[stepview_nwppck_ngn_getclassification2productrefs] gc2p
INNER JOIN [StepMirror].[dbo].[stepview_nwppck_ngn_getpimweblegalattrlist1] gpwa ON gc2p.partnumber=gpwa.PARTNUMBER and gpwa.ORDERBY='96'
INNER JOIN [StepMirror].[dbo].[stepview_nwppck_ngn_getclassification2assetrefs] gc2a ON gc2p.id=gc2a.id
INNER JOIN [StepMirror].[dbo].[stepview_nwppck_ngn_getproduct2assetrefs] gp2a ON gc2p.partnumber=gp2a.PRODUCTNAME
WHERE gc2p.id = 5665976 AND gp2a.assettype='Primary Image' AND gc2a.assettype='Primary Image'
AND(CASE WHEN ISDATE(ATTRIBUTEVALUE) = 0 then NULL ELSE ATTRIBUTEVALUE END) > dateadd(month,-12,getdate())
order by gc2p.orderby
The issue is likely that you have non-date data in the ATTRIBUTEVALUE field. You’re assuming that including the
ATTRIBUTENAME='New Date'will filter it, but it may not depending upon the query plan. Try…In complex joins, the SQL engine may decide to join the tables first then use the filter, which will cause a mismatch is not every instance of
ATTRIBUTEVALUEwill convert implicitly to a date.Also, please use real SQL joins, not comma joins. It’s bad form and causes way more problems than it solves.