SQL query if not null enter field value

265 views Asked by At

Trying to create SQL query where in reading a field if the field entry meets the condition or is null it enters the value read if not it skips the whole select for that line.

SELECT a.name, b.age 
CASE
    WHEN a.birthday = b.birthday THEN vlaue?
    WHEN a.birthday != b.birthday THEN skip?
    ELSE is null THEN null
END AS birthday

FROM information a, tracking b

WHERE a.name = b.name

So it would enter the name and age if the birth date matches or is a null but if the birth date does not match it would not enter name and age at all.

1

There are 1 answers

4
ScaisEdge On

You should use where for this

  SELECT a.name, b.age 
    CASE
        WHEN a.birthday = b.birthday THEN 'value'
        ELSE null
    END AS birthday

  FROM information a
  INNER JOIN  tracking b ON  a.name = b.name
  WHERE a.birthday != b.birthday

(and you should use explict join notation .. is more readble)

or if you need also when don't match use left join

 SELECT a.name, b.age 
    CASE
        WHEN a.birthday = b.birthday THEN 'value'
        ELSE null
    END AS birthday

  FROM information a
  LEFT JOIN  tracking b ON  a.name = b.name
  WHERE a.birthday != b.birthday