In SQL query check if a dateA field is equal to "01/01/1900" and if so it returns the value of another dateB field

1

I have a very long SQL query in one of its paragraphs I need to control if a field dateA is equal to "01/01/1900" and if so it returns the value of another field dateB.

As I commented the query is very very long with other CASE, arguments, etc. .. in between. Then between the code there is a piece with T1.FechaA, T2.Import ... and I have added T1.FechaA (CASE WHEN T1.FechaA = '1900-01-01' THEN T1.FechaB END), T2.Import. .. but I get an error

    
asked by Popularfan 05.09.2018 в 11:56
source

2 answers

5

Here you would have an example with CASE :
Where it will always show what is in the date field, unless it is equal to the date '01 / 01/1900 'which in that case will show you the value of dateB

select campo1, campo2, campo3, 
  case when fechaA = '01/01/1900'
  then fechaB
  else fechaA
  end AliasQueLeQuierasDar
from tabla1
where campo1 = condicion1

Alias, I'm not sure that SQLServer works, now I've been able to test it in an Oracle database, but the CASE statements are the same in both languages if I'm not wrong.

    
answered by 05.09.2018 / 12:05
source
2

Like this?

SELECT fechaB
FROM tabla
WHERE fechaA = '1900-01-01'

Or with a CASE if you want to keep all values in the query

SELECT
   CASE 
       WHEN fechaA = '1900-01-01' THEN fechaB
       ELSE fechaA
   END AS fechaX
FROM tabla
    
answered by 05.09.2018 в 12:05