SQLTeam.com | Weblogs | Forums

Divide by module

Hi All.
when I need to select odd or even records I'm using num%2 and check for 0 or 1.
Can somebody explain that expression: num%1?


% gets a remainder (modulo) from a division.

So, any_even_number % 2 returns 0, any_odd_number % 2 returns 1.

Hi ScottPletcher. Thanks for reply.
I don't have problem with expression %2. I'm trying to figure out how %1 works.


I would think %1 would always return zero. I don't see how any remainder could be left when dividing by 1.

It is a quick way of returning only the decimal portion of a numeric value:

Declare @myTable Table (MyDecimal decimal(5,3));
 Insert Into @myTable (MyDecimal)
 Values (12.123), (12.456), (12.567), (12.789);

 Select *
      , mod_1 = mt.MyDecimal % 1
   From @myTable mt;