Conditional NOT NULL case SQL

Spencer picture Spencer · Jan 27, 2011 · Viewed 81.3k times · Source

I am trying to calculate a field and I want it to behave differently depending on if one of the columns happens to be null. I am using MySQL

CASE 
  WHEN reply.replies <> NULL THEN
  24/((UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(qcr.LAST_MOD_TIME)+3600)/3600)*(ces.EXPERT_SCORE+2.5*scs.SIMILARITY)*(EXP(-reply.replies))
  ELSE 1
END as ANSWER_SCORE

Is this the right syntax?

Answer

Joe Stefanelli picture Joe Stefanelli · Jan 27, 2011

You need to have when reply.replies IS NOT NULL

NULL is a special case in SQL and cannot be compared with = or <> operators. IS NULL and IS NOT NULL are used instead.