tapos ghosh
tapos ghosh

Reputation: 2202

why MySQL return null 1000/0

Today I use a query select 1000/0 expected result must be error or exception but mysql return me null , can anyone tell me why . It return this message

Upvotes: 0

Views: 280

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270443

This is well-documented behavior:

By default, division by zero produces a result of NULL and no warning. By setting the SQL mode appropriately, division by zero can be restricted.

With the ERROR_FOR_DIVISION_BY_ZERO SQL mode enabled, MySQL handles division by zero differently:

  • If strict mode is not enabled, a warning occurs.

  • If strict mode is enabled, inserts and updates involving division by zero are prohibited, and an error occurs.

I would never rely on this behavior (simply because most other databases generate an error). Use:

select 100 / nullif(0, 0)

so you explicitly produce a NULL value instead of relying on the settings of the database.

Upvotes: 1

Related Questions