SQLite Mathematical Functions

Table of Contents

Absolute Value

The ABS() function in SQLite is used to return the absolute value of a number. It takes a single argument and returns its positive value. For example:

SELECT ABS(-5); -- Output: 5
SELECT ABS(10); -- Output: 10

Square Root

To find the square root of a number in SQLite, you can use the SQRT() function. It returns the square root of a given value. Here’s an example:

SELECT SQRT(25); -- Output: 5.0
SELECT SQRT(16); -- Output: 4.0

Exponential Function

The EXP() function in SQLite is used to calculate the exponential value of a given number. It returns e raised to the power of the argument. Here’s how you can use it:

SELECT EXP(2); -- Output: 7.38905609893065
SELECT EXP(0.5); -- Output: 1.64872127070013

Trigonometric Functions

SQLite provides various trigonometric functions, such as SIN(), COS(), and TAN(), to perform trigonometric calculations. These functions take an angle in radians as input and return the corresponding trigonometric value. Here are some examples:

SELECT SIN(0); -- Output: 0.0
SELECT COS(0); -- Output: 1.0
SELECT TAN(0); -- Output: 0.0

Ceiling and Floor

The CEIL() and FLOOR() functions in SQLite are used to round a number up or down, respectively. CEIL() returns the smallest integer greater than or equal to the specified value, while FLOOR() returns the largest integer less than or equal to it. Here’s how you can use these functions:

SELECT CEIL(4.2); -- Output: 5
SELECT FLOOR(4.9); -- Output: 4

Random Number Generation

SQLite provides the RANDOM() function, which generates a random floating-point value between 0 and 1. You can use this function to generate random numbers in your queries. Here’s an example:

SELECT RANDOM(); -- Output: Random decimal value between 0 and 1

Conclusion

SQLite’s mathematical functions offer powerful capabilities to perform calculations and manipulate numeric data within your SQL queries. Whether you need to find absolute values, calculate square roots, utilize trigonometric functions, round numbers, or generate random values, SQLite has you covered. Incorporate these functions into your code to enhance the mathematical capabilities of your SQLite database.

For a comprehensive list of mathematical functions supported by SQLite, refer to the official documentation.

Tags: #SQLite #MathematicalFunctions