Varchar to numeric conversion error in SQL query, references line with simple select clause?

38 views Asked by At

I created a fairly complex select query that works as expected in several of my Dynamics GP databases, but i came across one DB where it throws the below error

"Msg 8114, Level 16, State 5, Line 36 Error converting data type varchar to numeric."

i'm not converting anything on line 36, (double clicking the error takes me to that line) it's literally: "select distinct a.CUSTNMBR as 'Cust Num', "

That particular column is primarily varchar values (letters and numbers) but there are a few values that are completely numerical. i would think they would be treated as varchar though? I'm assuming that referenced line is not actually where that error is being triggered.

The fact that this query works on other DB's (which have basically the same schema) is really confusing. I am pulling from a custom view (that i didn't create) joined to several system tables, as the base data source in each DB. I'm wondering if that view could be the issue? could the view be somehow different on this particular DB? I'm not sure how to track that down. The create scripts that generate are exactly the same on each DB.

below is the actual query script

'Declare @MONTHEND as date

set @MONTHEND = '4/30/2023';
select distinct a.CUSTNMBR as 'Cust Num', 
a.CUSTNAME as 'Cust Name', 
a.DOCNUMBR as 'Document Num',
b.aaSubLedgerHdrID as 'Header ID',
a.posteddt as 'Posted Date', 
case when a.RMDTYPAL = 1 then 'Invoice'
when a.rmdtypal = '9' then 'Payment'
when a.rmdtypal = '7' then 'Credit Memo'
when a.rmdtypal = '3' then 'Debit Memo'
else 'N/A'
end  as 'Tran Type',
Case when a.rmdtypal in ('1','3') then a.ORTRXAMT*1
when a.rmdtypal in ('9','7') then a.ortrxamt*-1
else 'N/A'
end as 'Original Trx Amt', 
Case when g.APFRDCTY = '7' then 0
when g.apfrdcty = '9' then sum(g.APFRMAPLYAMT)
else 0
end as 'Payments',
Case when g.apfrdcty = '7' then sum(g.APFRMAPLYAMT)
when g.apfrdcty = '9' then 0
else 0
end
as 'CR Memo',
Case when a.rmdtypal in ('1','3') then a.CURTRXAM*1
when a.rmdtypal in ('9','7') then a.CURTRXAM*-1
else 'N/A'
end as 'Curr Trx Amt', 
c.aatrxdimcode as 'Project',
f.ACTNUMBR_2 as 'Natural',
sum(c.DR) as 'Debit',
sum(c.CR) as 'Credit'
 from slbreceivablestrx a
left join AAG20000 b on a.DOCNUMBR = b.DOCNUMBR and a.RMDTYPAL=b.DOCTYPE 
--left join AAG20001 c on b.aaSubLedgerHdrID = c.aaSubLedgerHdrID
--left join AAG20003 d on c.aaSubLedgerHdrID = d.aaSubLedgerHdrID and c.aaSubLedgerDistID =    d.aaSubLedgerDistID and d.aaTrxDimID = (select aaTrxDimID from AAG00400 where aaTrxDim = 'PROJECT')

--left join AAG00401 e on d.aaTrxDimID = e.aaTrxDimID and d.aaTrxCodeID = e.aaTrxDimCodeID
left join (select a.aaSubLedgerHdrID,INTERID, ACTINDX, DISTTYPE, sum(debitamt) as "DR", sum(CRDTAMNT) as "CR", aaTrxDimCode from AAG20001 a
left join AAG20003 b on a.aasubledgerhdrid =b.aasubledgerhdrid and a.aaSubLedgerDistID=b.aaSubLedgerDistID and b.aaTrxDimID = (select aaTrxDimID from AAG00400 where aaTrxDim = 'PROJECT')
left join AAG00401 e on b.aaTrxDimID = e.aaTrxDimID and b.aaTrxCodeID = e.aaTrxDimCodeID
Group by a.aaSubLedgerHdrID, INTERID,ACTINDX,DISTTYPE,aaTrxDimCode) c on    b.aaSubLedgerHdrID=c.aaSubLedgerHdrID
left join RM20201 g on a.DOCNUMBR = g.aptodcnm and a.CUSTNMBR=g.CUSTNMBR and g.ApplyFromGLPostDate <=     @MONTHEND or a.DOCNUMBR=g.APFRDCNM and a.CUSTNMBR = g.CUSTNMBR and g.ApplyFromGLPostDate <= @MONTHEND
join GL00100 f on c.ACTINDX = f.ACTINDX
where /*a.CURTRXAM <> 0 and */ c.DISTTYPE = 3 and f.ACTNUMBR_2 like '%1295%' and a.GLPOSTDT <= @MONTHEND and a.VOIDSTTS <> 1
group by a.CUSTNMBR, a.CUSTNAME, a.DOCNUMBR, a.POSTEDDT, c.aaTrxDimCode, f.ACTNUMBR_2,  a.RMDTYPAL,     g.APFRDCTY, a.ORTRXAMT, a.CURTRXAM,g.apfrmaplyamt,b.aaSubLedgerHdrID`
0

There are 0 answers