Skip to content

Files

Latest commit

Oct 31, 2024
9b31e45 · Oct 31, 2024

History

History
119 lines (89 loc) · 2.76 KB

postgresql-degrees.md

File metadata and controls

119 lines (89 loc) · 2.76 KB
title page_title page_description prev_url ogImage updatedOn enableTableOfContents previousLink nextLink
PostgreSQL DEGREES() Function
PostgreSQL DEGREES() Function
In this tutorial, you will learn how to use the PostgreSQL DEGREES() function to convert radians to degrees.
2024-02-18T03:54:44+00:00
true
title slug
PostgreSQL DIV() Function
postgresql-math-functions/postgresql-div
title slug
PostgreSQL EXP() Function
postgresql-math-functions/postgresql-exp

Summary: in this tutorial, you will learn how to use the PostgreSQL DEGREES() function to convert radians to degrees.

Introduction to the PostgreSQL DEGREES() function

The DEGREES() function converts radians to degrees. Here’s the syntax of the DEGREES() function:

DEGREES(radians_value)

In this syntax, the radians_value is a value in radians that you want to convert to degrees.

The DEGREES() function returns the value of the radians_value in degrees.

If the radians_value is NULL, the DEGREES() function returns NULL.

PostgreSQL DEGREES() function examples

Let’s take some examples of using the DEGREES() function.

1) Basic DEGREES() function examples

The following example uses the DEGREES() function to convert 1 radian to its equivalent degrees:

SELECT DEGREES(1);

Output:

      degrees
-------------------
 57.29577951308232
(1 row)

The following example uses the DEGREES() function to convert the value of π (pi) radians to its equivalent in degrees:

SELECT DEGREES(PI());

Output:

 degrees
---------
     180
(1 row)

Note that the PI() function returns the value of π (pi) radians.

2) Using the DEGREES() function with table data

First, create a new table called angles to store radian data:

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

Second, insert some rows into the angles table:

INSERT INTO angles (angle_radians)
VALUES
    (2*PI()),
    (PI()),
    (PI()/2),
    (NULL)
RETURNING *;

Third, use the DEGREES() function to convert radians to degrees:

SELECT
    id,
    angle_radians,
    ROUND(DEGREES(angle_radians)::numeric, 0) AS angle_degrees
FROM
    angles;

Output:

 id |  angle_radians   | angle_degrees
----+------------------+---------------
  1 | 6.28318530717959 |           360
  2 | 3.14159265358979 |           180
  3 |  1.5707963267949 |            90
  4 |             null |          null
(4 rows)

Summary

  • Use the PostgreSQL DEGREES() function to convert radians to degrees.