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. It accepts any number that can be implicitly converted to a float, and it always returns a positive number. 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 SQL 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 SQL SQRT and SQL LOG functions from the available SQL 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]

NOTE: Please use float variables as input. Otherwise, you may expect strange results from SQL Server.
Use SQUARE() with zero
If we pass zero, the SQUARE() function returns 0 as a result set.
SELECT SQUARE(0)
0
Using SQUARE() on NULL values
If we pass a NULL value, the SQUARE() function returns NULL as a result set.
SELECT SQUARE(NULL)
NULL
Using SQUARE() on negative numbers
When we use negative numbers, the SQUARE() function calculates the square of it and returns the positive output.
SELECT SQUARE(-4)
16
Let me use SQUARE() on a negative floating-point number.
SELECT SQUARE(-5.5)
30.25
Use SQUARE function on table columns
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 data in the table below. Refer to the SQL Server tutorial page.
SELECT [EnglishProductName]
,[Color]
,[StandardCost]
,[SalesAmount]
,SQUARE([SalesAmount]) AS Sales
FROM [Mathemetical Functions]
