Add Cast in SQL SERVER

0

Hello friends I want to make a sum of a series of elements that "cast" to decimal , this is what I am trying to do. But he says

  

Incorrect syntax near 'sum'.

select sum(  SELECT CAST(em.sueldoDiario AS DECIMAL(18, 4)) )

from
sysCPPuestos p
inner join sysCPCentroCosto cc
on cc.idSysCPEmpresas = p.idSysCPEmpresas
inner join sysCPEmpleados em
on em.idSysCPEmpresas = p.idSysCPEmpresas

where p.idSysCPEmpresas = 1
    
asked by E.Rawrdríguez.Ophanim 19.10.2018 в 18:03
source

3 answers

1

If you are not using a subquery, do not put the SELECT in CAST, that's why you are indicating the error, if you remove it your code should work, I hope it works for you

select sum(CAST(em.sueldoDiario AS DECIMAL(18, 4)) )

from
sysCPPuestos p
inner join sysCPCentroCosto cc
on cc.idSysCPEmpresas = p.idSysCPEmpresas
inner join sysCPEmpleados em
on em.idSysCPEmpresas = p.idSysCPEmpresas

where p.idSysCPEmpresas = 1
    
answered by 19.10.2018 / 18:08
source
1

You simply need to delete the second SELECT :

select sum( CAST(em.sueldoDiario AS DECIMAL(18, 4)) )
from
sysCPPuestos p
inner join sysCPCentroCosto cc
on cc.idSysCPEmpresas = p.idSysCPEmpresas
inner join sysCPEmpleados em
on em.idSysCPEmpresas = p.idSysCPEmpresas
where p.idSysCPEmpresas = 1
;
    
answered by 19.10.2018 в 18:08
1

Try as follows:

SELECT CAST(SUM(em.sueldoDiario AS DECIMAL(18, 4)))
FROM
sysCPPuestos p
inner join sysCPCentroCosto cc
on cc.idSysCPEmpresas = p.idSysCPEmpresas
inner join sysCPEmpleados em
on em.idSysCPEmpresas = p.idSysCPEmpresas

WHERE p.idSysCPEmpresas = 1

The CAST must contain the SUM .

I hope it serves you. Greetings!

    
answered by 19.10.2018 в 18:12