SQL sqrt Function | Square Root of a Number

SQL sqrt is an inbuilt Math Function in SQL Server. It calculates the square root of an input number in SQL Server. In this article, we will discuss the SQL sqrt Function. Also, we will discuss a few examples of using it.

Syntax

SQRT (number)

Parameters

The function expects only one input. The number for which you wish to calculate the square root is the parameter to the function. Also, negative numbers do not have a square root. Therefore, you should pass a positive number in the input.

Return Value

The function returns the square root of the input number.

Input: sqrt(25)
Output: 5

Input: sqrt(0)
Output: 0

Input: sqrt(-9)
Output: Null

Input: sqrt(5) 
Output: 2.2360679774998
SQL SQRT Function

Support

The function is supported in SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse, and Parallel Data Warehouse.

Examples

Let’s discuss a few examples of using the function to find the square root of a number in SQL.

Example 1: Square Root of a Positive Number

mysql> SELECT SQRT(4);
+---------+
| SQRT(4) |
+---------+
|       2 |
+---------+

mysql> SELECT SQRT(25);
+----------+
| SQRT(25) |
+----------+
|        5 |
+----------+

mysql> SELECT SQRT(0.25);
+------------+
| SQRT(0.25) |
+------------+
|        0.5 |
+------------+

mysql> SELECT SQRT(5);
+------------------+
| SQRT(5)          |
+------------------+
| 2.23606797749979 |
+------------------+

Example 2: Square Root of a Negative Number

Negative numbers do not have a Square Root. It returns Null in MySQL. However, on Microsoft SQL Server, it throws an SQLSTATE Error.

MySQL
mysql> SELECT SQRT(-10);
+-----------+
| SQRT(-10) |
+-----------+
|      NULL |
+-----------+

Microsoft SQL Server
Error: SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]An invalid floating point operation occurred.

Example 3: Square Root of 0

When 0 is passed as a parameter, the function returns 0.

mysql> SELECT SQRT(0);
+---------+
| SQRT(0) |
+---------+
|       0 |
+---------+

Conclusion

In this article, we discussed the SQL sqrt Function. You can read more about it on the Official Microsoft Documentation. Additionally, you can also read about SQL Math Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *