Spencer
Spencer

Reputation: 22370

IFNull in MySQL

I am new to SQL and I am trying to create a calculated field using a set of columns. However, some of those values may be NULL. If they are NULL, I don't want the calculated field to return a NULL result but instead set some arbitrary value.

Here is the calculated field

(ces.EXPERT_SCORE * cirm.CONSUMER_RATING) + (12.5 * scs.SIMILARITY)

Upvotes: 2

Views: 1371

Answers (2)

Chandu
Chandu

Reputation: 82903

You can use COALESCE operator

(
 COALESCE(ces.EXPERT_SCORE, <YOUR_ARBIT_VALUE>) * 
 COALESCE(cirm.CONSUMER_RATING, <YOUR_ARBIT_VALUE>)
)
+ (12.5 * COALESCE(scs.SIMILARITY, <YOUR_ARBIT_VALUE>))

Upvotes: 0

FeRtoll
FeRtoll

Reputation: 1267

use IFNULL(tocheckwhat,withwhattoreplace) so:

(IFNULL(ces.EXPERT_SCORE,5) * IFNULL(cirm.CONSUMER_RATING,5)) + (12.5 * IFNULL(scs.SIMILARITY,5))

should work! :)

Upvotes: 1

Related Questions