How do I generate random number for each row in a TSQL Select?

前端 未结 19 1230
逝去的感伤
逝去的感伤 2020-11-22 07:03

I need a different random number for each row in my table. The following seemingly obvious code uses the same random value for each row.

SELECT table_name,          


        
19条回答
  •  无人共我
    2020-11-22 07:43

    select round(rand(checksum(newid()))*(10)+20,2)
    

    Here the random number will come in between 20 and 30. round will give two decimal place maximum.

    If you want negative numbers you can do it with

    select round(rand(checksum(newid()))*(10)-60,2)
    

    Then the min value will be -60 and max will be -50.

提交回复
热议问题