PostgreSQL RADIANS() Function


PostgreSQL RADIANS() Function

The PostgreSQL RADIANS() function is used to convert an angle measured in degrees to an angle measured in radians. This function is essential for working with trigonometric data where conversion from degrees to radians is required.


Syntax

RADIANS(degrees)

The RADIANS() function has the following component:

  • degrees: The angle in degrees to be converted to radians.

Example PostgreSQL RADIANS() Queries

Let's look at some examples of PostgreSQL RADIANS() function queries:

1. Basic RADIANS() Example

SELECT RADIANS(180) AS radians;

This query converts the angle 180 degrees to radians, which is approximately 3.14159.

2. RADIANS() with Column Values

SELECT degrees, RADIANS(degrees) AS radians
FROM angles;

This query retrieves the degrees and their corresponding radians from the angles table.

3. RADIANS() with Negative Values

SELECT degrees, RADIANS(degrees) AS radians
FROM angles
WHERE degrees < 0;

This query retrieves the degrees and their corresponding radians from the angles table where the degrees are negative.


Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the RADIANS() function to convert angles from degrees to radians.

Step 1: Creating a Table

This step involves creating a new table named angles to store angle data.

CREATE TABLE angles (
    id SERIAL PRIMARY KEY,
    degrees NUMERIC
);

In this example, we create a table named angles with columns for id and degrees.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the angles table.

INSERT INTO angles (degrees)
VALUES (0),
       (45),
       (90),
       (180),
       (-45);

Here, we insert data into the angles table.

Step 3: Using the RADIANS() Function

This step involves using the RADIANS() function to convert the angles from degrees to radians in the angles table.

-- Basic RADIANS()
SELECT degrees, RADIANS(degrees) AS radians
FROM angles;

-- RADIANS() with Negative Values
SELECT degrees, RADIANS(degrees) AS radians
FROM angles
WHERE degrees < 0;

These queries demonstrate how to use the RADIANS() function to convert the angles from degrees to radians in the angles table, including basic usage and handling negative values.

Conclusion

The PostgreSQL RADIANS() function is a fundamental tool for converting angles from degrees to radians. Understanding how to use the RADIANS() function and its syntax is essential for effective data retrieval and manipulation in PostgreSQL databases.