SQL Server數學函式

2021-09-06 10:29:20 字數 2579 閱讀 6546

1、計算絕對值abs

abs函式對乙個數值表示式結果計算絕對值(bit資料型別除外),返回整數。

語法結構:

abs(數值表示式)

返回值:與數值表示式型別一致的資料

示例:

select

abs(-

1) --

輸出 1

2、獲取大於等於最小整數值celling

ceiling函式返回大於等於數值表示式(bit資料型別除外)的最小整數值。俗稱天花板函式,盡量往大的取整。

語法結構:

ceiling(數值表示式)

返回值:與數值表示式型別一致的資料。

示例:

select

celling(123.1) --

輸出124

3、獲取小於等於最大整數值floor

floor函式返回小於等於數值表示式(bit資料型別除外)的最大整數值。俗稱地板函式,盡量往小的取整。

語法結構:

floor(數值表示式)

返回值:

與數值表示式型別一致的資料。

示例:

select

floor(123.9999) --

輸出123

4、獲取隨機數rand

rand函式返回隨機的從0到1之間的浮點數。

語法結構:

rand([

seed

])

語法中的seed是種子值,是乙個整數型別的表示式(tinyint、smallint、int),使用指定的種子值反覆呼叫rand將返回相同的結果、。

不指定種子值,rand函式將隨機產生結果。

返回值:

0到1之間的浮點數。

示例:

select

rand() --

輸出 0.36925407993302

5、獲取指定長度和精度round

round函式對數值表示式捨入到指定長度和精度。

語法結構:

round( numeric_expression , length[

, function

] )

引數說明:

numeric_rxpression:精確或近似數值型別的表示式,bit資料型別除外。

length:捨入精度。length必須是tinyint、smallint或int。如果length為整數,則將numeric_expression捨入到length指定的小數字數。如果length為負數,則將numeric_expression小數點左邊部分捨入到length指定的長度。

function:要執行的操作的型別。function必須為tinyint、smallint或int。如果省略function或其值為0(預設值),則將捨入numeric_expression。如果指定了0以外的值,則將截斷numeric_expression。

返回值:與numeric_expression相同型別的數值。

示例:

select

round(123.456789,3) --

輸出 123.457000 精確到小數點後3位

6、degrees

返回弧度對應的角度

select

degrees(0.6) --

34.377467707849391000

7、pi

返回圓周率float

select

pi() --

3.14159265358979

8、power

返回指定表示式的指定冪的值。

select

power(2,10) --

1024

9、square

返回指定浮點值的平方。

select

square(5) --

25

10、sqrt

返回指定浮點值的平方根(開方)。

select

sqrt(25) --

5

11、exp

返回指定的 float 表示式的指數值。

12、sin  asin

以近似數字 (float) 表示式返回指定角度(以弧度為單位)的三角正弦值。

13、cos  acos

14、tan atan atn2

15、cot

16、log  log10

17、sign

18、radians

SQL Server 數學函式

常見的數學函式有 絕對值函式 abs x 和返回圓周率的函式 pi 平方根函式 sqrt x 獲取隨機數的函式 rand 和rand x 四捨五入函式 round x,y 符號函式 sign x 獲取整數的函式 ceiling x 向上取整和 floor x 向下取整 冪運算函式 power x,y...

Sql Server函式全解《二》數學函式

原文 sql server函式全解 二 數學函式 eg select abs 2 abs 3.3 abs 33 pi eg select rand rand rand 可以看到,不帶引數的rand 每次產生的隨即數值是不同的。eg select rand 10 rand 10 rand 11 可以看...

SQLServer自定義函式學習

sql server 2000 允許使用者建立自定義函式,自定義函式可以有返回值。自定義函式分為 標量值函式或表值函式 如果 returns 子句指定一種標量資料型別,則函式為標量值函式。可以使用多條 transact sql 語句定義標量值函式。如果 returns 子句指定 table 則函式為...