HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Simplify Oracle SQL: Treat null date as 'N' and non-null date as 'Y'

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
nonsqlnulldatesimplifyandoracletreat

Problem

This works, but nvl AND case seem redundant.
Basically, if null then 'N' else 'Y.

CHICKEN_HATCH_DATE is a DATE data type.

select  stricken_chicken_id,
          case nvl(to_char(chicken_hatch_date),'N') 
          when 'N' then 'N'
          else 'Y'
          end chicken_hatch_date              
   from   stricken_chicken;

Solution

I'm not sure what's wrong with your code, but if you don't like all of those 'N''s would

select  stricken_chicken_id,
          case when to_char(chicken_hatch_date) is null then
             'N'
          else
             'Y'
          end chicken_hatch_date              
   from   stricken_chicken;


work for you?

Code Snippets

select  stricken_chicken_id,
          case when to_char(chicken_hatch_date) is null then
             'N'
          else
             'Y'
          end chicken_hatch_date              
   from   stricken_chicken;

Context

StackExchange Code Review Q#13052, answer score: 4

Revisions (0)

No revisions yet.