SQL Server PI() Function

Savigo
You organize your data. Now organize your savings.
Set savings goals, track your progress, and stay on course.
Get Savigo for iPhone 

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()

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;

Output:

pi
-----------------
3.14159265358979

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;

Output:

62.8319

How it works.

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

DECLARE @radius FLOAT;
SET @radius = 10.0;

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

DECLARE @circumference FLOAT;
SET @circumference = 2 * PI() * @radius;

Third, display the circumference:

PRINT @circumference;

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
);

Second, insert rows into the circles table:

INSERT INTO
  circles (radius)
VALUES
  (1.5),
  (2.5),
  (3),
  (5);

Third, retrieve data from the circles table:

SELECT * FROM circles;

Output:

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

(4 rows affected)

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

SELECT
  PI() * radius * radius area
FROM
  circles;

Output:

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

(4 rows affected)

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;

Output:

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

(4 rows affected)

Summary #

  • Use the PI() function to get the constant value of Pi.

Was this tutorial helpful?