0
In a consultation SQL was trying to work out a way to select a value from another column B if the selected column value To is null. I first made the following query:
SELECT COALESCE(NULLIF(max(convert(smalldatetime,DEP.DATAENTRADA,100)),null), DEP.DATANASCIMENTO,100) as UltimoDependente
from DEPENDENTES DEP
join FUNCIONARIOS FUNC on FUNC.FILIAL=DEP.FILIAL and FUNC.MATRICULA=DEP.MATRICULA and FUNC.D_E_L_E_T_ = ' '
where FUNC.MATRICULA = '10'
and FUNC.FILIAL = '01'
and DEP.R_E_C_D_E_L_=0
And the query doesn’t work. Is there any way to do this?
You want to return the highest value of the field
DEP.DATAENTRADA, and if this field is null you want to return the field valueDEP.DATANASCIMENTO, is that it? They have different types?– Pedro Gaspar
no. I want to return the last date. If the DATAINPUT field is not null, I use it to make the query. Now if the field does not have a registered date, ie null, I want to use the field DATANASCIMENTO
– Moisolas
Have you tried using Coalesce alone? Like this: SELECT COALESCE(SUA_DATA_ENTRADA, DATANASCIMENTO)
– Rodrigo Rocha
What mistake it makes?
– Rodrigo Rocha