Link to home
Start Free TrialLog in
Avatar of tmajor99
tmajor99

asked on

SQL Select to conditionally set a column to null based on a value from another column

I need a conditional SQL Select.  I need to conditionally set a column to null based on the value of another column.  For example;

Table
 
ID                      Shelf Life Flag                Shelf Life Days
-------------    ---------------                -----------------
100                       Y                                    10
200                       N                                    NULL
300                     (blank)                             NULL


I need to set the Shelf Life Days column to NULL if Shelf Life Flag is set to "Y".
ASKER CERTIFIED SOLUTION
Avatar of David Todd
David Todd
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tmajor99
tmajor99

ASKER

Yes your solution is a good recommendation but i settled on using case like this:

SELECT  A.[Batch ID], A.[Shelf Life Days]
        , CASE A.[Lot Control]
        WHEN 'Y'  THEN [Shelf Life Days]
        ELSE NULL
        END [Lot Control],
    CASE A.[Lot Control]
    WHEN 'Y'  THEN [Shelf Life Days]
        ELSE NULL
        END [Shelf Life Days]
    from [EGP_SYSTEM_ITEMS_INTERFACE] as A

Please ignore the syntax differences.