0

I have a table below. I need when indicator = 0 then date = null

I have tried a case statement but receive errors. Any help would be appreciated.

     Select 
       Case when a.indicator = 0
          Then a.date = 0
             Else a.date
                End Date

      From TableA a

TableA

   Id         Date     Indicator
    1          1/1/17      0
John
  • 289
  • 3
  • 14

5 Answers5

0
select
  decode(a.indicator, 0, null, a.date) date
from
  tableA
Sanders the Softwarer
  • 2,478
  • 1
  • 13
  • 28
  • [Don't use `DECODE()`](https://stackoverflow.com/a/13716116/458741) it's not ANSII standard and there's a few gotchas. It's best to prefer CASE – Ben Nov 20 '17 at 14:34
0

Use this select :

SELECT 
CASE 
  WHEN A.INDICATOR = 0 THEN 
    NULL
  ELSE A.DATE
END DATE
FROM TABLEA A
Mehdi Ghasri
  • 488
  • 3
  • 10
0

What you want is:

SELECT CASE WHEN a.indicator = 0 THEN NULL ELSE a.date END AS 'Date' FROM TableA AS a

The reason a.date = 0 (or date = null) is wrong is that you are not assigning a new value to a.date but rather stating what value (null) should be returned from the case expression when it is true.

jpw
  • 44,361
  • 6
  • 66
  • 86
0

You can use this :

Select "Id", 
    Case 
        when "Indicator" <> 0 
            Then "Date" End AS Date1, 
    "Indicator"
From TableA;

Please notice I have reversed your logic and skipped the else null because this is anyway default if not used.

More about case: http://modern-sql.com/feature/case

SQL HERE

Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43
0
Select 
    Case a.indicator when 0
        Then null
            Else a.date
            End
 From TableA a
Shammas
  • 381
  • 1
  • 4
  • 15