SQL SQUARE Function

The SQL Square Function is a mathematical function used to calculate the square of the specified expression or number. The syntax of the SQUARE Function is

SELECT SQUARE (Float_Expression)
FROM [Source]

This Function calculates the square of a given numeric value. The following SELECT query will show multiple ways to use the SQUARE function.

SQL Server SQUARE Function Example

We used this one to calculate the square of the variable @i and assigned a new name using the ALIAS Column. It means, @i * @i = 2 * 2 = 4

In the next SELECT statement, we used the integer value as input for this method. It means, SQUARE(@j) = @j * @j => 2.20 * 2.20 = 4.84. But we are getting 4 as a result because it is rounding the 2.20 value to 2. Please refer to the SQRT and LOG functions from the available Mathematical Functions.

DECLARE @i float = 2, @j int = 2.20

SELECT SQUARE(@i) AS [SQLSQUARES]

-- Calculating directly
SELECT SQUARE(3.20) AS [SQLSQUARES]

-- Wrong Value
SELECT SQUARE(@j) AS [SQLSQUARE]
SELECT SQUARE(2.20) AS [SQLSQUARE]
SQUARE Function Example 1

NOTE: Please use float variables as input. Otherwise, you may expect strange results from SQL Server.

This math Function also allows you to calculate the square of column values. In this example, we will calculate the square of all records present in [Sales Amount]. For this demo, we use the below table data.

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