sql 函数 杂记

来源:互联网 发布:淘宝产品推广咋挣钱 编辑:程序博客网 时间:2024/05/01 04:45
CEILING (Transact-SQL)

 

Returns the smallest integer greater than, or equal to, the specified numeric expression.

Topic link iconTransact-SQL Syntax Conventions

Syntax

Copy
CEILING (numeric_expression )
Arguments

numeric_expression

Is an expression of the exact numeric or approximate numeric data type category, except for the bit data type.

Return Types

Returns the same type as numeric_expression.

Examples

The following example shows positive numeric, negative, and zero values with the CEILING function.

Copy
SELECT CEILING($123.45), CEILING($-123.45), CEILING($0.0)GO

Here is the result set.

Copy
--------- --------- ------------------------- 124.00    -123.00    0.00                     (1 row(s) affected)
_____________________________________________________________________________________________

比较 CEILING 和 FLOOR

CEILING 函数返回大于或等于所给数字表达式的最小整数。FLOOR 函数返回小于或等于所给数字表达式的最大整数。例如,

对于数字表达式 12.9273,CEILING 将返回 13,FLOOR 将返回 12。FLOOR 和 CEILING 返回值的数据类型都与输入

的数字表达式的数据类型相同。
----------------------------------------------------------------------------------
现在,各位就可以根据自己需要使用这两种方法来取得随机数了^_^

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

 

 

使用 RAND

RAND 函数计算 0 到 1 之间的随机浮点数,而且可选择以 tinyintintsmallint 值作为要计算的随机数的起始点。

下面的示例计算两个随机数。第一个 RAND() 函数让 Microsoft® SQL Server™ 选取种子值,第二个 RAND() 函数使用值 3 作为起始位置。

SELECT RAND(), RAND(3)

RAND 函数是伪随机的数字生成器,它用与 C 运行时库 rand 函数类似的方法进行操作。如果没有提供种子值,

系统将生成它自己的不定种子值。如果用种子值调用 RAND,则必须使用不定的种子值来生成随机数。

如果用同一种子值多次调用 RAND,它将返回相同的生成值。下面的脚本中,对 RAND 的调用都返回相同的值,

这是因为它们都使用了相同的种子值:

 

 

原创粉丝点击