Use cast for a filter on sql server

3
 select  (select NOMCLI from CARROS where cast(IDCLI as int) = idreg1) as NOMCLI, fecha 
  from replog 
    where movimiento = 'ALT' and TABLA = 'CARROS' and usuario = 'sa'

I have an example here where idcli is integer: 327 and idreg1 is decimal: 327.00.

I make that query but the nomcli brings them all null, how would you apply the cast to do the filter? or if there is another way.

    
asked by Alcides Salazar 28.12.2016 в 21:07
source

1 answer

2

First of all, the comparison should work as it is (although I do not understand why you are converting IDCLI to whole, if it was already that type of data).

Anyway, the best way to do this is with a JOIN , not a subquery:

SELECT  c.NomCli,
        r.fecha
FROM replog r
LEFT JOIN carros c
    ON IdCli = CAST(idreg1 AS INT)
WHERE movimiento = 'ALT' 
AND TABLA = 'CARROS' 
AND usuario = 'sa';
    
answered by 28.12.2016 / 21:15
source