SQL SQRT Function

The SQL Server SQRT Function finds the square root of the specified expression or numeric number. The syntax of the SQRT Function to find the square root is

SELECT SQRT (Float_Expression)
FROM [Source]

The SQRT() function accepts an expression of a float data type, calculates the square root, and returns a float.

For this demo, we use the Mathematical table data.

SQL Mathematical Functions

SQL Server SQRT Function Example

The SQRT Function finds the square root of a given numeric value. The following Mathematical query will show multiple ways to use this one.

Finding the square root of a number

We declared a float and an int variable and assigned the values. Next, we used the SQL Server math function to calculate the square root of the variable @i. It means √4 = 2. Please refer to the list of SQL Math functions from our SQL Server Programming page.

DECLARE @i float = 4
SELECT SQRT(@i) AS [Res]

Finding the square root of a decimal number

In the next SQL SELECT statement, we used the integer value as input for this method. It means, SQRT(@j) = √2.20 = 1.483. But we are getting 1.4142 as a result because it is rounding the 2.20 value to 2, and the square root of 2 is 1.4142

DECLARE @j int = 2.20

-- Calculating directly
SELECT SQRT(9.90) AS [SQLSQRT]

-- Wrong Value
SELECT SQRT(@j) AS [SQLSQRT]
SELECT SQRT(2.20) AS [SQLSQRT]
SQL SQRT FUNCTION 1

NOTE: Please use float variables as input for the SQL Server SQRT Function. Otherwise, you may expect strange results. Explore the SQL SQUARE function.

Using the SQRT() function with negative numbers

If we use the SQRT() function with a negative value, it returns an error: An invalid floating-point operation occurred.

SELECT SQRT(-4) AS Res
Msg 3623, Level 16, State 1, Line 1
An invalid floating point operation occurred.

Using the SQRT() function with NULL

When we pass a NULL value, the SQRT() function returns NULL as the result set.

SELECT SQRT(NULL)
NULL

Finding the square root of numbers in a range

In the query below, we used a SQL while loop to iterate from 4 to 10. Inside it, we used the SQRT() function to find the square root of numbers between 4 and 10 (4, 5, 6, 7, 8, 9, and 10).

DECLARE @N FLOAT = 4
WHILE @N <= 10
BEGIN
	SELECT SQRT(@N) AS S_ROOT
	SET @N = @N + 1
END
S_ROOT
----------------------
2

(1 row affected)

S_ROOT
----------------------
2.23606797749979

(1 row affected)

S_ROOT
----------------------
2.44948974278318

(1 row affected)

S_ROOT
----------------------
2.64575131106459

(1 row affected)

S_ROOT
----------------------
2.82842712474619

(1 row affected)

S_ROOT
----------------------
3

(1 row affected)

S_ROOT
----------------------
3.16227766016838

Use SQL SQRT() to find the distance between two points

If we have coordinates of (2, 4) and (10, 20), we can use the following query to find the distance between the two points.

SELECT SQRT(POWER(10 - 2, 2)) + SQRT(POWER(20 - 4, 2)) AS Distance
Distance
24

Use SQRT() function on table columns

The SQRT Function also allows you to find the square root of column values. Here, we will calculate the square of all records present in [Sales Amount] using it. Please refer to the SQL EXP and SQL POWER functions.

SELECT [EnglishProductName]
      ,[Color]
      ,[StandardCost]
      ,[SalesAmount]
      ,SQRT([SalesAmount]) AS Sales
 FROM [Mathemetical Functions]
SQRT FUNCTION 2
Categories SQL