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]

SQL Server SQUARE Function Example

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

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 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

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]
SQL SQUARE Function Example 1

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

This SQL Server math Function also allows you to calculate the square of column values. In this example, we will calculate the square of All the records present in [Sales Amount].

For this demo, we use the below table data.

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