SQL Server PI() Function

Summary: in this tutorial, you will learn how to use the SQL Server PI() function to obtain the constant value of Pi.

Introduction to SQL Server PI() function

In math, Pi is a constant representing the ratio of a circle’s circumference to its diameter. Pi is typically denoted by the Greek letter π

Pi approximately equals 3.14159, even though its decimal representation can go on infinitely without repeating.

In SQL Server, you can use the PI() function to obtain the constant value of PI.

Here’s the syntax of the PI() function:

PI()Code language: JavaScript (javascript)

The PI() function returns the constant value of Pi with an accuracy of 15 digits.

SQL Server PI() function examples

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

1) Basic PI() function example

The following statement uses the PI() function to get the Pi constant:

SELECT PI() pi;Code language: SQL (Structured Query Language) (sql)

Output:

pi
-----------------
3.14159265358979Code language: JavaScript (javascript)

2) Using the PI() function to calculate the circumference of a circle

The following example uses the PI() function to calculate the circumference of a circle:

DECLARE @radius FLOAT;
SET @radius = 10.0; 

-- Calculating the circumference using PI() function
DECLARE @circumference FLOAT;
SET @circumference = 2 * PI() * @radius;

-- Display the result
PRINT @circumference;Code language: SQL (Structured Query Language) (sql)

Output:

62.8319Code language: JavaScript (javascript)

How it works.

First, declare a variable to store the radius of a circle:

DECLARE @radius FLOAT;
SET @radius = 10.0;Code language: SQL (Structured Query Language) (sql)

Second, calculate the circumference of a circle using the PI() function:

DECLARE @circumference FLOAT;
SET @circumference = 2 * PI() * @radius;Code language: SQL (Structured Query Language) (sql)

Third, display the circumference:

PRINT @circumference;Code language: SQL (Structured Query Language) (sql)

3) Using the PI() function with table data

First, create a table called circles that stores the radiuses of circles:

CREATE TABLE circles(
   id INT IDENTITY PRIMARY KEY,
   radius DEC(10,2) NOT NULL
);Code language: SQL (Structured Query Language) (sql)

Second, insert rows into the circles table:

INSERT INTO
  circles (radius)
VALUES
  (1.5),
  (2.5),
  (3),
  (5);Code language: SQL (Structured Query Language) (sql)

Third, retrieve data from the circles table:

SELECT * FROM circles;Code language: SQL (Structured Query Language) (sql)

Output:

id          radius
----------- -------
1           1.50
2           2.50
3           3.00
4           5.00

(4 rows affected)Code language: JavaScript (javascript)

Finally, calculate the area of circles based on radiuses stored in the circles table:

SELECT
  PI() * radius * radius area
FROM
  circles;Code language: SQL (Structured Query Language) (sql)

Output:

area
-----------------
7.06858347057703
19.6349540849362
28.2743338823081
78.5398163397448

(4 rows affected)Code language: JavaScript (javascript)

To make the areas more readable, you can use the ROUND() function to round the area to numbers with two precisions and use the CAST function to cast them to numbers with two decimal places:

SELECT
  CAST(ROUND(PI() * SQUARE(radius), 2) AS DEC (5, 2)) area
FROM
  circles;Code language: SQL (Structured Query Language) (sql)

Output:

area
---------
7.07
19.63
28.27
78.54

(4 rows affected)Code language: JavaScript (javascript)

Summary

  • Use the PI() function to get the constant value of Pi.
Was this tutorial helpful?