更多數學函式

SQL Server 中級

Ginger Grant

Instructor

絕對值

使用 ABS() 回傳非負值

ABS(number)
SQL Server 中級

在 T-SQL 使用 ABS(I)

SELECT ABS(-2.77), ABS(3), ABS(-2)
+--------------------+-------------------+-------------------+
|(No column name)    |(No column name)   |(No column name)   |
+--------------------+-------------------+-------------------+
|2.77                |3                  |2                  |
+--------------------+-------------------+-------------------+

SQL Server 中級

在 T-SQL 使用 ABS(II)

SELECT DurationSeconds, ABS(DurationSeconds) AS AbsSeconds 
FROM Incidents
+--------------------+--------------------+ 
|DurationSeconds     |AbsSeconds          | 
+--------------------+--------------------+ 
|-25.36              |25.36               | 
|-258482.44          |258482.44           |
|45.66               |45.66               |
+--------------------+--------------------+ 
SQL Server 中級

平方與平方根(T-SQL)

SELECT SQRT(9) AS Sqrt, 
       SQUARE(9) AS Square
+--------+-----------+ 
|Sqrt    |Square     |
+--------------------+
|3       |81         |
+--------+-----------+

SQL Server 中級

對數

  • LOG() 回傳自然對數
  • 你也可指定底數,未指定時為 2.718281828

 

LOG(number [,Base])
SQL Server 中級

在 T-SQL 計算對數

SELECT  DurationSeconds, LOG(DurationSeconds, 10) AS LogSeconds
FROM Incidents
+--------------------+--------------------+  
|DurationSeconds     |LogSeconds          |
+--------------------+--------------------+  
|37800               |4.577491799837225   |
|5                   |0.6989700043360187  |
|20                  |1.301029995663981   |
...
+--------------------+--------------------+  
SQL Server 中級

0 的對數

你不能對 0 取對數,否則會發生錯誤。

SELECT LOG(0, 10)

An invalid floating point operation occurred.
SQL Server 中級

一起來練習吧!

SQL Server 中級

Preparing Video For Download...