In PL/SQL, the SQRT function is used to find the square root of a number. This function is really handy for various tasks that involve mathematical calculations, such as analyzing statistics, solving geometry problems, or handling financial data.
The SQRT function is easy to use and can simplify complex calculations right within your SQL queries and PL/SQL code. By using the SQRT function, developers can make their data processing more efficient and perform accurate mathematical operations directly in their database applications.
In this article, we will explore the SQRT function in PL/SQL with examples and discuss its advantages.
SQRT Function in PL/SQL
In PL/SQL, the SQRT
function is used to compute the square root of a number. This mathematical function is helpful when you need to perform calculations that involve the square root, such as in statistical analysis, geometry, or financial calculations.
Syntax:
SQRT( number )
- number: The numeric value for which you want to calculate the square root. It must be a non-negative number.
Return Value:
This function returns a numeric number which is the square root of the given input number.
Supported Versions of Oracle/PLSQL are given below:
- Oracle 12c
- Oracle 11g
- Oracle 10g
- Oracle 9i
- Oracle 8i
Examples of PLSQL SQRT Function
Suppose you want to calculate the square root of a number, say 16. You can use the SQRT function as follows:
Example 1
DECLARE
result NUMBER;
BEGIN
result := SQRT(16);
DBMS_OUTPUT.PUT_LINE('The square root of 16 is: ' || result);
END;
Output:
The square root of 16 is: 4
Explanation: In this example, the square root of 16 is calculated to be 4, which is printed to the console.
Example 2
DECLARE
Test_Number number := 5.617;
BEGIN
dbms_output.put_line(SQRT(Test_Number number));
END;
Output:
2.37002109695251
Explanation: In this example, the square root of 5.617 is calculated as approximately 2.37002109695251.
Example with Table Data
Let's say you have a table named 'numbers
'
with a column 'value
'
that contains various numbers. You want to calculate the square root for each number in the table.
Table Structure:
CREATE TABLE numbers (
id NUMBER,
value NUMBER
);
Inserting Data:
INSERT INTO numbers (id, value) VALUES (1, 25);
INSERT INTO numbers (id, value) VALUES (2, 9);
INSERT INTO numbers (id, value) VALUES (3, 4);
Query to Calculate Square Roots:
SELECT id, value, SQRT(value) AS square_root
FROM numbers;
Output:
ID | VALUE | SQUARE_ROOT
---|-------|------------
1 | 25 | 5
2 | 9 | 3
3 | 4 | 2
Explanation: Here, the SQRT
function is applied to each value in the numbers
table, returning the square root for each entry. For example, the square root of 25 is 5, the square root of 9 is 3, and the square root of 4 is 2.
Handling Negative Numbers
The SQRT
function in PL/SQL does not handle negative numbers and will return NULL
for such values. If you attempt to compute the square root of a negative number, it will result in a NULL value.
Example
DECLARE
result NUMBER;
BEGIN
result := SQRT(-9);
DBMS_OUTPUT.PUT_LINE('The square root of -9 is: ' || result);
END;
Output:
The square root of -9 is:
Explanation: Since the square root of a negative number is not defined in the set of real numbers, the function returns 'NULL'.
Advantages of Using the SQRT Function
- Ease of Use: Simplifies the process of calculating square roots directly in SQL and PL/SQL.
- Integration: Can be seamlessly integrated into SQL queries and PL/SQL blocks, enhancing data processing capabilities.
- Versatility: Useful in a wide range of applications, from basic arithmetic to complex mathematical computations.
- Error Handling: Returns '
NULL
'
for negative inputs, which can be handled gracefully in your application logic.
Conclusion
The SQRT
function in PL/SQL is a straightforward yet powerful tool for performing square root calculations. It is used to find the principal square root of a non-negative number. When working with real-world data, ensure that the input to the SQRT
function is non-negative to avoid unexpected results. This function is beneficial in various scenarios, from basic arithmetic operations to more complex mathematical computations in database applications.
Similar Reads
PLSQL | TAN Function
The TAN function is an inbuilt function in PLSQL which is used to return the tangent of an input number. The tangent is a trigonometric function of an angle and here the input number is the angle expressed in the form of radians. 180 degree is equal to pi radian. Syntax: TAN( number ) Parameters Use
1 min read
PLSQL | SIN Function
The PLSQL SIN function is used to return the sine of a numeric value. The SIN function accepts one parameter which is the number whose sine needs to be calculated. The SIN function returns a value of the numeric data type. This function takes as an argument any numeric data type as well as any non-n
2 min read
PLSQL | SUBSTR Function
The PLSQL SUBSTR function is used for extracting a substring from a string. The SUBSTR function accepts three parameters which are input_string, start_position, length. SUBSTR calculates lengths using characters as defined by the input character set. SUBSTRB uses bytes instead of characters. Note: I
2 min read
PLSQL | SIGN Function
The PLSQL SIGN function is used for returning a value which indicates the sign of a number. The SIGN function accepts one parameter which is the number whose sign needs to be known. The SIGN function returns a value of the numeric data type. This function takes as an argument any numeric data type a
2 min read
PLSQL | TANH Function
The PLSQL TANH function is used to return the hyperbolic tangent of a numeric value. The TANH function accepts one parameter which is the number whose hyperbolic tangent needs to be calculated. The TANH function returns a value of the numeric data type. This function takes as an argument any numeric
2 min read
PLSQL | TRIM Function
The PLSQL TRIM function is used for removing all specified characters either from the beginning or the end of a string. The TRIM function accepts three parameters among which the first parameter can either have one of the values 'LEADING', 'TRAILING', 'Both' and Trim_character and input_string. If L
2 min read
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 min read
PLSQL | POWER Function
The POWER function is an inbuilt function in PLSQL which is used to return the calculated value when a raised to the bth power. If a is negative number, then b must be an integer. Syntax: POWER(a, b) Parameters Used: This function accepts two parameters a and b. If a is negative number, then b must
2 min read
PLSQL | TRUNC Function
The TRUNC function is an inbuilt function in PLSQL which is used to return a number truncated to a particular number of decimal places. Syntax: TRUNC( number, decimal_places ) Parameters Used: This function accepts two parameters which are illustrated below:- number - This is the input number which
2 min read
PLSQL | LN Function
The LN function is an inbuilt function in PLSQL which is used to return the natural logarithm of a given input number. The natural logarithm of a number is the logarithm of that number to the base e, where e is the mathematical constant approximately equal to 2.718. This is written using the notatio
2 min read