SQUARE: Expect a float Result From Numeric Inputs

SQUARE returns float even when the supplied value uses an exact numeric type. I keep that conversion visible instead of assuming the function preserves its input’s decimal contract.

Nine wooden cubes in a square arrangement on a workshop bench beside two trays and a closed book.
Nine wooden cubes laid out in a three by three square on a workshop bench.

A familiar operation can have a different type

Squaring a number means multiplying it by itself. SQL expressions can perform that operation through multiplication or through SQUARE. The mathematical intent can match while the type contract differs. That distinction matters before substituting one expression for another.

SQUARE accepts float or any input that converts implicitly to float. Its native return type is float. An int or decimal argument therefore doesn’t request an int or decimal result. This article doesn’t apply POWER’s separate return-type rules to SQUARE.

I’d check the actual function rather than infer its behavior from the name. A concise expression can introduce approximate arithmetic into an otherwise exact calculation. That may be acceptable for a measurement. It needs a different review for exact accounting or identifiers.

Keep the original numbers beside their squares

The demonstration uses negative three, negative 1.25, zero and positive two. A fifth input is NULL. Each input is explicitly decimal(7,2). The result is cast to decimal(12,6) for a fixed display contract.

The non-NULL results are nine, 1.5625, zero and four. A negative input has a positive square because both factors are negative. The final decimal representation keeps six fractional digits. That displayed width doesn’t describe the native result type.

A metadata property column identifies the native type as float for the non-NULL expressions. The missing input produces NULL for both the result and property. Those outputs keep missingness separate from a measured zero. No replacement policy is applied.

WITH Inputs AS
(
    SELECT Id, CAST(InputValue AS decimal(7,2)) AS InputValue
    FROM (VALUES (1, -3.00), (2, -1.25), (3, 0.00),
                 (4, 2.00), (5, NULL)) AS v(Id, InputValue)
)
SELECT Id, InputValue, CAST(SQUARE(InputValue) AS decimal(12,6)) AS DisplaySquare,
       CONVERT(varchar(20), SQL_VARIANT_PROPERTY(
           SQUARE(InputValue), 'BaseType')) AS NativeType
FROM Inputs
ORDER BY Id;
Native SSMS results showing squares of negative, fractional, zero and positive inputs, with float as the native type and NULL preserved.
Negative input still produces a nonnegative square. The fractional input -1.25 produces 1.562500, and the native SQUARE result type is float. NULL remains NULL. Open the result at full size.

Choose exact multiplication when exactness is required

An explicitly typed multiplication expression has its own precision and scale rules. It can be appropriate when the calculation must remain exact decimal arithmetic. Its available width still needs review. Overflow doesn’t disappear merely because float was avoided.

I can defend SQUARE for a calculation whose source and destination already use approximate numbers. That choice fits the same numerical contract. Replacing an exact decimal calculation requires stronger justification. A successful example with small values isn’t enough.

The sample values are safely within its selected display width. Larger inputs can exceed either a calculation or final conversion range. This article makes no unlimited-range claim. Choose real input limits and output precision before applying the pattern broadly.

SQUARE returns float, so check first

Verify the stated output contract

Compare the final decimal values rather than demanding unrestricted equality between floating intermediates. That narrow contract is deliberate. It makes the selected display values reviewable. It doesn’t establish arbitrary approximation bounds for all possible inputs.

The complete script reads a literal VALUES list through a CTE. It creates no objects or state changes. ORDER BY fixes the five-row sequence. Input, displayed square and native-type metadata appear together in each output tuple.

Compare every result, base-type property and SQL type across the complete input set. Keep the original value beside its square. Retain the NULL row during that check. A count of four visible numbers would omit part of the stated input contract.

Use SQUARE where float is fine, and say so out loud.

A familiar square is not a guaranteed exact numeric result, it is an operation that returns float.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL Datatype, SQL Function, SQL NULL, SQL Server
Previous Post
SQL SERVER – Query to Find First and Last Day of Current Month – Date Function
Next Post
Last Line of a SQL Script: The Joke Every DBA Has Lived

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.