SQL SQUARE Function

The SQL SQUARE Function is a SQL Mathematical Function that is used to calculate the square of the specified expression or number. The syntax of the SQL Server SQUARE Function is

SELECT SQUARE (Float_Expression)
FROM [Source]

SQL SQUARE Function Example 1

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

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

SELECT SQUARE(@i) AS [SQL SQUARES]

-- Calculating SQUARE directly
SELECT SQUARE(3.20) AS [SQL SQUARES]

-- Wrong Value
SELECT SQUARE(@j) AS [SQL SQUARE]
SELECT SQUARE(2.20) AS [SQL SQUARE]
SQL SQUARE FUNCTION 1

We used the SQUARE function to calculate the square of the variable @i, and assigned a new name using the ALIAS Column.

SELECT SQUARE(@i) AS [SQL SQUARES]

It means, @i * @i = 2 * 2 = 4

In the next statement, We used the integer value as input for the SQUARE function.

-- Wrong Value
SELECT SQUARE(@j) AS [SQL SQUARES]
SELECT SQUARE(2.20) AS [SQL SQUARES]

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

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

SQUARE Function Example 2

The Square 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] using SQUARE Function.

For this Sql Server SQUARE Function demo, we use the Math Table data

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