Skip to content
Advertisement

Real, mathematical “modulo” operation in Postgres?

In Ruby:

-2 % 24
=> 22

In Postgres:

SELECT -2 % 24;
 ?column?
----------
      -2

SELECT mod(-2,24);
 mod
-----
  -2

I can easily write one myself, but I’m curious whether Postgres has a real modulus operation, as opposed to remainder after division.

Advertisement

Answer

SELECT MOD(24 + MOD(-2, 24), 24);

will return 22 instead of -2

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement