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".
SQL

Avatar of undefined
Last Comment
tmajor99

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
David Todd

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23