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.
SQL SQRT() Function Syntax
The basic syntax of the SQRT Function in SQL Server is as shown below:
SELECT SQRT (Float_Expression) FROM [Source]
In this article we will show you, How to write SQRT Function in SQL Server to find the square root with example. For this, We are going to use the below shown data
SQL SQRT Function Example 1
The SQRT Function in SQL is used to find the square root of a given numeric value. The following query will show multiple ways to use 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]
Below lines of code is used to declare float and int variable and assigning the values.
DECLARE @i float = 4, @j int = 2.20
In the below statement, We used SQL Server SQRT function to calculate the square root of the variable @i. We also assigned new name to that result as ‘SQL SQRTI’ using SQL ALIAS Column.
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]
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.
SQL SQRT Function Example 2
The Square Root Function (SQRT) also allows you to find the square root of a column values. In this example, We are going to calculate the square of All the records present in [Sales Amount] using SQRT Function.
SELECT [EnglishProductName] ,[Color] ,[StandardCost] ,[SalesAmount] ,SQRT([SalesAmount]) AS Sales FROM [Mathemetical Functions]
Thank You for Visiting Our Blog