I was looking at postgres question and it called for updating a table with random values.
I only noticed it was targeted at postgres after answering it, but in answering it I hit a mystery.
Here is my sample code in question:
create table #Buildings([Use] varchar(50), n int)
insert #Buildings
select null,null from sysobjects
update #Buildings
set [Use] =
case (ABS(CAST(CAST(NEWID() AS VARBINARY) AS int)) % 6)
when 0 then null
when 1 then 'warehouse'
when 2 then 'office'
when 3 then 'market'
when 4 then 'retail'
when 5 then 'workshop'
else 'HOW IS THIS POSSIBLE'
end,
n = ABS(CAST(CAST(NEWID() AS VARBINARY) AS int)) % 6
select [Use], count(*) from
#Buildings
group by [Use]
select n, count(*) from
#Buildings
group by n
It returns a very odd result set:
Use
-------------------------------------------------- -----------
workshop 128
HOW IS THIS POSSIBLE 633
NULL 287
retail 140
warehouse 258
market 177
office 209
And a second result set that makes complete sense:
n
----------- -----------
0 292
3 300
1 313
4 277
5 311
2 339
The data for both result sets was generated in the same update statement.
So my question is why is a number out of the range of 0 - 5 hitting my case statement? What is that number? Why when I update the int directly is stuff distributed properly?