# Setting Rows to Odd and Even Values

• Comments posted to this topic are about the item Setting Rows to Odd and Even Values

• Just a quick note on odd/even numbers.  The quickest way to find out if a number is odd or even is to use a modulo function.  If you take a number, n, and mod by 2 it returns 0 if even and 1 if odd.  For example:

SELECT 3 AS number, 3 % 2 AS IsOdd

UNION all

SELECT 15, 15 % 2

union all

SELECT 159670, 159670 % 2;

• Thanks for your comment.  It is true that if you are just trying to determine if a number is odd or even use modulo function.

In my case, as you can see in my article, the issue isn't trying to figure out if something is odd or even, but to force 1,2,3,4,5 to be 1,3,5,7,9

Or to force 1,2,3,4,5 to be 2,4,6,8,10

So then when you sort those rows together the records are interleaved.  There is no way to use modulo to do that.

Thanks,

Ben

• Nicely done, Ben.  Awesome simplification of an "interleaving" method using some very straight-forward math.

--Jeff Moden

RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

Change is inevitable... Change for the better is not.

Helpful Links:
How to post code problems
How to Post Performance Problems
Create a Tally Function (fnTally)

• Thanks Jeff.

Ben

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply