Save Big on Cyber Monday! Up to 40% Off
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

SQRT() Function in SQL Server: Syntax, Examples, and Use Cases

Introducing SQRT(), which represents the square root function, is one unique change that completely transforms calculating square roots in SQL Server. SQRT() is the inverse of the square function—SQUARE()—in SQL. With SQRT(), you no longer have to worry about runtime errors and misleading results during complex mathematical operations.

SQRT() is a built-in function that lets you return the square root of any non-negative number without using any extra tools. Since its emergence, SQRT() has grown as an efficient function for calculating square roots in SQL across various data-driven fields in addition to SQUARE(), which calculates the square in SQL.

Whether you are a database developer, DBA, or data analyst, this guide provides the knowledge you need to work with the SQRT() function. It covers the explanation of how the SQRT() differs from the square function in SQL and how it works in SQL Server. Significantly, dbForge Studio for SQL Server is a multipurpose IDE with advanced features that let you handle complex mathematical tasks efficiently. With this tool, you can visualize the result of your queries and understand your data at a deeper level.

Let's get started.

SQL Server SQRT() function syntax

The SQRT() function is used to calculate the square root of a non-negative number in SQL Server. Meaning, the numeric expression for this function must be non-negative. When you apply it to a non-negative value, it returns results as a float, while for negative values, it throws a runtime error, which can be handled with input validation.

Here is the breakdown of its syntax and how it works.

Basic syntax

The typical syntax for the SQRT() square root function in SQL Server is as follows:

SELECT SQRT(numeric_expression);

The numeric_expression in this syntax represents the number whose square root you want to calculate. It must be non-negative values; otherwise, the function will return NULL. For instance, if you want to calculate the square root of 49, which is 7, 49 is the numeric expression. Here is the syntax for this scenario.

SELECT SQRT(49);

Additionally, SQRT() in SQL can handle fractional square roots with precision. Regardless of the numeric expression, the command would return a result that is a FLOAT. For example, the square root of 2 returns approximately 1.4142135.

Note
A numeric expression can be any valid numeric data type like INT, DECIMAL, and FLOAT.

Parameters explained

Parameters are like the numeric expression of the SQRT() function. They refer to the numeric data types that the function accepts. Meaning the different data types that you can use as numeric expressions when performing the SQRT() and the square function in SQL.

The table below explains the different parameters you can use for your SQRT().

Data type Description
numeric_expression The number whose square root you want to calculate. Must be non-negative.
INT Whole numbers. Works well for basic square root operations.
FLOAT A floating-point number that supports decimals. Ideal for precise square root calculations.
DECIMAL / NUMERIC Fixed-precision numbers. Useful when decimal accuracy and rounding control are required.
REAL Single-precision floating-point. Less precise than FLOAT, but compatible.
SMALLINT Smaller-range integers. Valid input for SQRT() if non-negative.
TINYINT Small positive integers (from 0 to 255). Safe to use with SQRT().
BIGINT Large whole numbers. Supported by SQRT() for high-range values.

Understanding these parameters allows you to use SQRT() confidently for both simple and complex analytical calculations.

SQRT() function examples

Now that you understand how SQRT() in SQL Server works and the parameters you can use, let's consider a few examples.

Using SQRT() with literal values

You can use SQRT() with hardcoded numeric values in a simple SELECT statement like this:

SELECT SQRT(16) AS SquareRoot;

Output:

4

The square root of 16 is 4, and since SQRT() returns a float, the result is shown as 4.0. This is a quick and effective way to test how the function behaves with literal inputs.

Using SQRT() on table columns

In large datasets with numerical values, you can use the SQRT() math function in SQL to process a column in your table. For instance:

SELECT  
    ProductID,  
    Price,  
    SQRT(Price) AS PriceRoot 
FROM  
    Products;

This query retrieves the product ID and original price. Then it calculates the square root of each product's price using the SQRT() function. The result is useful for performing statistical or financial analysis directly within your SQL environment.

Using SQRT() with expressions

The SQRT() square root function in SQL can also be applied to more complex mathematical expressions. For example, you can use this to calculate the Euclidean distance from the origin (0,0) to a point (3,4). Here is the syntax for this scenario:

SELECT SQRT(POWER(3, 2) + POWER(4, 2)) AS Distance;

Output:

5

This query calculates √(3² + 4²) → √(9 + 16) → √25, which equals 5. It demonstrates how SQRT() integrates smoothly with other math functions in SQL like POWER() to solve real-world problems, such as computing distances or applying geometric formulas.

Behavior with edge cases and errors

Although using the SQRT() function is relatively straightforward, with edge cases and errors, the behavior is different. Edge cases refer to unusual or boundary input values that remain within valid data types but may lead to different behavior.

Below are common edges and inputs that return NULL in SQRT().

  • Negative numbers (e.g., SQRT(-1)): While negative numbers are considered as numerically valid inputs, since their square roots are not real numbers, SQL Server throws a runtime error, especially when you run this without validating input.
  • Zero (e.g., SQRT(0)): This is another numerically valid case that results in 0 but is sometimes overlooked.
  • Very large or small numbers: These are technically valid, but they may reveal rounding errors or floating-point limitations in extreme scenarios.
  • NULL inputs: When you input NULL as the numeric expression of your SQRT() function, the result will also be NULL (not technically an "error," but it may cause logic issues if unhandled).

Furthermore, errors in the SQRT() function are often returned as NULL, not as traditional errors like a divide-by-zero would return.

What happens with negative inputs?

When you attempt to calculate the square root of a negative number in SQL Server using the SQRT() function, it doesn't return NULL; it throws a runtime error. This is because SQL Server does not support imaginary or complex numbers.

Example:

SELECT SQRT(-1) AS Result;

Output:

Msg 3623, Level 16, State 1, Line 1    
An invalid floating point operation occurred.

So instead of silently failing or returning NULL, SQL Server stops execution and raises an error for invalid input.

To avoid this error and ensure your queries run safely, you should always validate inputs before passing them to SQRT(). You can do this using a WHERE clause or a CASE expression.

Using the WHERE clause:

SELECT SQRT(Value) AS SafeRoot 
FROM MyTable 
WHERE Value >= 0;

Using a CASE statement:

SELECT  
    CASE  
        WHEN Value >= 0 THEN SQRT(Value) 
        ELSE NULL  
    END AS SafeRoot 
FROM MyTable;

These techniques help filter or control negative inputs before passing them to the SQRT() function.

Handling NULLs with input data

When you pass a NULL as the numeric expression to calculate a square root in SQL using the SQRT() function, SQL Server returns NULL as the result. For instance:

SELECT SQRT(NULL) AS Result;

Output:

NULL
Note
In situations like this, if you're working with data where NULLs are possible (e.g., missing measurements or optional fields), you’ll need to handle them intentionally, perhaps with ISNULL(), COALESCE(), or conditional logic, depending on your use case.

Real-world use cases of SQRT() in SQL Server

Here are some real-world use case scenarios where you can use SQRT().

Calculating Euclidean distance

The SQRT() function is a core part of the Euclidean distance formula. It is often used in scenarios like the following:

  • Geospatial calculations (e.g., measuring distances between coordinates).
  • Machine learning (e.g., calculating distances between data points in feature space).
  • Clustering algorithms like K-means.

Here's a simplified example for two points (x1, y1) and (x2, y2) stored in a table:

SELECT  
    SQRT(POWER(x2 - x1, 2) + POWER(y2 - y1, 2)) AS Distance 
FROM  
    Coordinates;

This query calculates the straight-line distance between the two points. It is useful in location-based services, pattern recognition, and more.

Financial calculations

In finance and investment analysis, the SQRT() square root function in SQL can be used in risk modeling and statistical calculations such as standard deviation, volatility, and Sharpe ratio components.

For example, to compute the standard deviation of returns, here is how your query would look as follows:

SELECT  
    SQRT(SUM(POWER(Return - AvgReturn, 2)) / COUNT(*)) AS StdDev 
FROM  
    PortfolioReturns;

This query measures how spread out the returns are, giving investors insight into risk and variability.

Quality control and manufacturing

SQRT() in SQL is also useful for analyzing process variability in Six Sigma and Statistical Process Control (SPC), especially when calculating any of the following processes:

  • Standard deviation of defects
  • Root mean square deviation
  • Capability indices (e.g., Cp, Cpk)

Using SQRT() in this scenario helps manufacturers monitor and improve product consistency, reduce defects, and maintain high-quality standards.

Example:

SELECT  
    SQRT(SUM(POWER(Measurement - AvgMeasurement, 2)) / COUNT(*)) AS ProcessStdDev 
FROM  
    QualityCheck;

SQRT() vs other SQL Server math functions

No doubt, SQRT() in SQL Server is more than just a math function. It's a valuable building block for data-driven decision-making. However, the SQRT() function is only one of the math functions in SQL Server. There are other math functions like POWER(x, y), ABS(x), and FLOOR(x).

Knowing when and how to use these functions, especially in combination, can help you write cleaner and more effective SQL queries.

The table below shows the comparison of some common math functions in SQL Server.

Function Description Example Result
SQRT(x) Returns the square root of a non-negative number SQRT(25) 5
POWER(x, y) Returns x raised to the power y POWER(25, 0.5) 5
ABS(x) Returns the absolute (positive) value of x ABS(-9) 9
ROUND(x, d) Rounds x to d decimal places ROUND(SQRT(2), 2) 1.41
FLOOR(x) Rounds x down to the nearest whole number FLOOR(SQRT(10)) 3
CEILING(x) Rounds x up to the nearest whole number CEILING(SQRT(10)) 4

SQRT() vs POWER()

You can technically calculate SQL square roots using either SQRT(x) or POWER(x, 0.5). For instance:

SELECT SQRT(64) AS UsingSQRT, POWER(64, 0.5) AS UsingPOWER;

For both scenarios, the result is:

8

Key differences:

  • SQRT(x) is more readable and clearly expresses intent when working with square roots.
  • POWER(x, 0.5) is more flexible, especially when dealing with fractional exponents beyond square roots.
  • Both return the same result for valid, non-negative inputs, but SQRT() is generally preferred for clarity.

When to use ABS(), ROUND(), FLOOR() with SQRT()

Usually, in SQL Server, the SQRT() function needs to be combined with other math functions to produce cleaner or more usable outputs.

Here are a few combinations of these functions and the appropriate situations for using them.

1. ABS() + SQRT()

Use this combination when your input values might be negative, but you still want to get valid results. For instance:

SELECT SQRT(ABS(Value)) AS SafeRoot 
FROM MyTable;

This query avoids NULL results from negative inputs.

2. ROUND() + SQRT()

Use this combination when you need a precise, formatted result (e.g., for reporting or financial data). For instance:

SELECT ROUND(SQRT(2), 3) AS RoundedRoot;

Output:

1.414

3. FLOOR() / CEILING() + SQRT()

Use this function when you need whole-number approximations of square roots. For instance:

SELECT FLOOR(SQRT(10)) AS FloorRoot, CEILING(SQRT(10)) AS CeilRoot;

Output:

3 and 4
Note
Combining SQRT() with other math functions in SQL helps you to gain better control over precision, formatting, and error handling. It also helps to ensure your SQL query is clean and efficient.

Conclusion

The SQRT() square root function in SQL Server is a very effective tool for computing square roots right in your queries. With this function, you can get accurate SQL square roots without using any other tools or doing math by hand, whether you're analyzing large datasets, building financial models, or performing quality control.

To explore and test math functions like SQRT() more effectively, try the dbForge Studio for SQL Server. This solution is built with an intuitive SQL editor, instant result previews, and intelligent code suggestions. With these features, you can write, visualize, and optimize your queries with confidence.

FAQ

Can I nest SQRT inside other functions?

Yes, you can nest SQRT() inside other SQL Server functions. For example, you can round its result using ROUND(SQRT(10), 2) or use it within arithmetic expressions like SQRT(POWER(x, 2) + POWER(y, 2)) for distance calculations.

Can I use SQRT on a column?

Absolutely. You can apply SQRT() to any numeric column in a SELECT statement. For instance:

SELECT ProductID, SQRT(Price) AS PriceRoot FROM Products;

This query calculates the square root of the Price column for each row.

How do I round the results of an SQRT function?

You can round the result using the ROUND() function. Example:

SELECT ROUND(SQRT(2), 3) AS RoundedRoot;

This query returns the square root of 2 rounded to three decimal places.

What does SQL return for the SQRT of a negative number?

SQL Server throws a runtime error when you attempt to calculate the square root of a negative number. But you can handle such cases with validation logic, like WHERE clauses or CASE statements.