The SQL SQRT Function is one of the Mathematical Function, which is used to find the square root of the specified expression or numeric number. The syntax of the SQL Server SQRT Function to find the square root is
SELECT SQRT (Float_Expression) FROM [Source]
For this Sql Server SQRT Function demo, we use the below-shown data
SQL SQRT Function Example 1
The SQRT Function finds the square root of a given numeric value. The following Mathematical Function query will show multiple ways to use the SQRT function.
DECLARE @i float = 4, @j int = 2.20 SELECT SQRT(@i) AS [SQL SQRT] -- Calculating SQRT directly SELECT SQRT(9.90) AS [SQL SQRT] -- Wrong Value SELECT SQRT(@j) AS [SQL SQRT] SELECT SQRT(2.20) AS [SQL SQRT]
OUTPUT
ANALYSIS
Below lines of code are used to declare a float, int variable and assigning the values.
DECLARE @i float = 4, @j int = 2.20
We used the SQL Server SQRT function to calculate the square root of the variable @i
SELECT SQRT(@i) AS [SQL SQRTI]
It means, √4 = 2
In the next statement, We used the integer value as input for the SQRT function.
-- Wrong Value SELECT SQRT(@j) AS [SQLSQRT] SELECT SQRT(2.20) AS [SQL SQRT]
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
NOTE: Please use float variables as an input for SQRT Function. Otherwise, you may expect strange results.
SQRT Function Example 2
The Square Root Function (SQRT) also allows you to find the square root of column values. In this example, We are going to calculate the square of All the records present in [Sales Amount] using the SQRT Function.
SELECT [EnglishProductName] ,[Color] ,[StandardCost] ,[SalesAmount] ,SQRT([SalesAmount]) AS Sales FROM [Mathemetical Functions]
OUTPUT