Adding any positive number to the result of a Subquery
returns as null
and needs to return 0 so that it can be added with the first value obtained.
What can I do to get the query to return an integer value?
Follow Query:
DECLARE @TOTAL VARCHAR(100)
SET @TOTAL = (SELECT TOP 1 PTB.PATSLDCTAVALCONTAB
FROM PAT_SALDO_CONTA PTB WITH (NOLOCK), CONTAB_SALDO_MOV_CTA CS WITH (NOLOCK)
WHERE CS.PlanoCtaCodRed = PTB.PlanoCtaCodRed
AND PTB.PlanoCtaCodRed = '18872'
AND PTB.PatSldCtaAnoMes = '201809'
AND PTB.EmpCod like '%' + (CASE WHEN '01.13' = '01' THEN '01.' ELSE '01.13' END)+ '%')
+(SELECT @TOTAL
+ COALESCE((SELECT (SUM(ISNULL(CSA.ContabSaldoMovCtaValDeb,0)))
-(SUM(ISNULL(CSA.ContabSaldoMovCtaValCred,0)))
FROM CONTAB_SALDO_MOV_CTA CSA
WHERE CSA.PlanoCtaCodRed = '18872'
AND CSA.ContabSaldoMovCtaAnoMes >= '2018' + '' + '10'
AND CSA.ContabSaldoMovCtaAnoMes <= '2018' + '' + '10'
AND CSA.EmpCod like '%' + (CASE WHEN '01.13' = '01' THEN '01.' ELSE '01.13' END) + '%')
,0))
SELECT @TOTAL