Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate a certain range of random numbers in SQL Server

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge about "how to generate a certain range of random numbers in SQL Server". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

method one

select floor(rand()*N) ---The number generated is: 12.0select cast( floor(rand()*N) as int) ---The number generated is: 12

methodology II

select ceiling(rand() * N) ---The number generated is: 12.0select cast(ceiling(rand() * N) as int) ---The number generated is: 12

where N is an integer you specify, such as 100.

At first glance, there was no difference between the two methods. Was there really no difference? In fact, there is one point, and that is the range of random numbers they generate:

The numerical range of method 1:0 to N-1, such as cast( floor(rand()*100) as int) will generate any integer between 0 and 99

Method 2: Number range: 1 to N, such as cast(ceiling(rand() * 100) as int) will generate any integer between 1 and 100

For this distinction, see SQL's online help:

------------------------------------------------------------------------------------

Compare CEILING and FLOOR

The CEILING function returns the smallest integer greater than or equal to the given numeric expression. The FLOOR function returns the largest integer less than or equal to the given numeric expression. For example, for the numeric expression 12.9273, CEILING returns 13 and FLOOR returns 12. Both FLOOR and CEILING return values of the same data type as the input numeric expression.

----------------------------------------------------------------------------------

Now, you can use these two methods to get random numbers according to your needs ^_^

In addition, but also to remind you, about the random access table arbitrary N records method, very simple, just use newid():

select top N * from table_name order by newid() ---N is an integer you specify, the table is the number of records to get "How to generate a certain range of random numbers in SQL Server" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report