The RADIANS()
function in SQLite is a mathematical function that converts an angle from degrees to radians. This can be handy when working with trigonometric calculations in your database queries, as some mathematical functions (such as SIN()
, TAN()
) expect angles in radians rather than degrees.
Syntax
RADIANS(angle_in_degrees)
Where angle_in_degrees
is a numeric expression representing an angle in degrees that you want to convert to radians.
How It Works
The function uses the mathematical formula:
radians = degrees × (π/180)
Example 1
Here’s a simple example to demonstrate:
SELECT radians(23);
Output:
+-------------------+
| radians(23) |
+-------------------+
| 0.401425727958696 |
+-------------------+
This is the equivalent of doing the following:
SELECT 23 * pi() / 180;
Output:
+-------------------+
| 23 * pi() / 180 |
+-------------------+
| 0.401425727958696 |
+-------------------+
Using the RADIANS()
function helps our code to be more concise.
Example 2
Let’s say you’re working with a table of angles and need to convert them to radians for further calculations:
-- Create a sample table
CREATE TABLE angles (
angle_id INTEGER PRIMARY KEY,
angle_degrees FLOAT
);
-- Insert some sample data
INSERT INTO angles (angle_degrees) VALUES
(0),
(45),
(90),
(180),
(360);
-- Convert angles to radians
SELECT
angle_degrees,
RADIANS(angle_degrees) as angle_radians
FROM angles;
Output:
+---------------+-------------------+
| angle_degrees | angle_radians |
+---------------+-------------------+
| 0.0 | 0.0 |
| 45.0 | 0.785398163397448 |
| 90.0 | 1.5707963267949 |
| 180.0 | 3.14159265358979 |
| 360.0 | 6.28318530717959 |
+---------------+-------------------+
Rounding the Results
We can use the ROUND()
function to round the results if required:
SELECT
angle_degrees,
ROUND(RADIANS(angle_degrees), 2) as angle_radians
FROM angles;
Output:
+---------------+---------------+
| angle_degrees | angle_radians |
+---------------+---------------+
| 0.0 | 0.0 |
| 45.0 | 0.79 |
| 90.0 | 1.57 |
| 180.0 | 3.14 |
| 360.0 | 6.28 |
+---------------+---------------+
Passing the Wrong Argument Type
Passing an argument of the wrong type results in NULL
:
SELECT radians('ten');
Output:
+----------------+
| radians('ten') |
+----------------+
| null |
+----------------+