開始使用免費開始

使用指數函式

當你需要在資料庫中進行計算時,指數函式非常實用。以儲存不動產資訊的資料庫為例,你可能需要計算面積。此時,以下函式會很有幫助:

  • POWER(number, power):將數值提升到指定的指數
  • SQUARE(number):將數值提升到 2 次方

另外,如果你需要計算已知座標的兩座城市之間的距離,也可以使用這個函式:

  • SQRT(number):計算正數的平方根。

在這個練習中,你會操作這些指數函式,並分析它們回傳的結果。

本練習屬於課程

SQL Server 的資料操作函式

檢視課程

練習說明

  • 將變數 @number 中的數值提升到變數 @power 指定的次方。
  • 計算變數 @number 的平方(也就是 2 次方)。
  • 計算變數 @number 中數值的平方根。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

DECLARE @number DECIMAL(4, 2) = 4.5;
DECLARE @power INT = 4;

SELECT
	@number AS number,
	@power AS power,
	-- Raise the @number to the @power
	___ AS number_to_power,
	-- Calculate the square of the @number
	___ num_squared,
	-- Calculate the square root of the @number
	___ num_square_root;
編輯並執行程式碼