惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - dnddn

axios输出图片显示 ElementUI时间选择控件提交的时间为UTC时间 pypi清华镜像 JS引用传值问题 通过Access-Control-Allow-Origin解决跨域问题 解决mac下mysql libssl.1.0.0.dylib报错问题 Google App Engine初探 SQL2005的阀值设定 移动硬盘识别不了的问题 网球术语英汉对照 【丰富词汇量,喜欢网球的路过说声。。】 几个小问题 - dnddn - 博客园 SilverLight,有多少人关心呢? be my friend 检讨 简单的就是最好的 这个五一 紧张非封闭式开发中 静静的看twittervision SQL Server 2005 数据类型
MSSQL中的随机函数
dnddn · 2007-05-26 · via 博客园 - dnddn

随机函数:rand()
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数。那就看下面的两种随机取整数的方法:

1、
A:select  floor(rand()*N)  ---生成的数是这样的:12.0
B:select cast( floor(rand()*N) as int)  ---生成的数是这样的:12

2、
A:select ceiling(rand() * N)  ---生成的数是这样的:12.0
B:select cast(ceiling(rand() * N) as int)  ---生成的数是这样的:12

    其中里面的N是一个你指定的整数,如100,可以看出,两种方法的A方法是带有.0这个的小数的,而B方法就是真正的整数了。
    大致一看,这两种方法没什么区别,真的没区别?其实是有一点的,那就是他们的生成随机数的范围:
方法1的数字范围:0至N-1之间,如cast( floor(rand()*100) as int)就会生成0至99之间任一整数
方法2的数字范围:1至N之间,如cast(ceiling(rand() * 100) as int)就会生成1至100之间任一整数
对于这个区别,看SQL的联机帮助就知了:
------------------------------------------------------------------------------------

比较 CEILING 和 FLOOR

CEILING 函数返回大于或等于所给数字表达式的最小整数。FLOOR 函数返回小于或等于所给数字表达式的最大整数。例如,对于数字表达式 12.9273,CEILING 将返回 13,FLOOR 将返回 12。FLOOR 和 CEILING 返回值的数据类型都与输入的数字表达式的数据类型相同。
----------------------------------------------------------------------------------
现在,各位就可以根据自己需要使用这两种方法来取得随机数了^_^

另外,还要提示一下各位菜鸟,关于随机取得表中任意N条记录的方法,很简单,就用newid():
select top N *  from table_name order by newid() ----N是一个你指定的整数,表是取得记录的条数.