Summary: in this tutorial, you will learn how to use the SQL Server SIN()
function to calculate the sine of a number.
Introduction to the SQL Server SIN() function
In SQL Server, the SIN()
function is a math function that returns the sine of a number.
Here’s the syntax of the SIN()
function:
SIN(n)
Code language: SQL (Structured Query Language) (sql)
In this syntax:
n
is the angle in radians for which you want to find the sine value.
The SIN()
function returns the sine of the argument n. If n is NULL
, the SIN()
function returns NULL
.
SQL Server SIN() function examples
Let’s explore some examples of using the SIN()
function.
1) Basic SIN() function example
The following example uses the SIN()
function to calculate the sine of the number 1 radian:
SELECT SIN(1) as sine_value;
Code language: SQL (Structured Query Language) (sql)
Output:
sine_value
-----------------
0.841470984807897
Code language: SQL (Structured Query Language) (sql)
2) Using the SIN() function with PI() function
The following example uses the SIN()
function to find the sine of pi/6:
SELECT SIN(PI()/6) sine_value;
Code language: SQL (Structured Query Language) (sql)
Output:
sine_value
-----------
0.5
Code language: SQL (Structured Query Language) (sql)
3) Using SIN() function with table data
First, create a table called angles
to store angles in radians:
CREATE TABLE angles(
id INT IDENTITY PRIMARY KEY,
angle DEC(19,2)
);
Code language: SQL (Structured Query Language) (sql)
Second, insert some rows into the angles
table:
INSERT INTO
angles (angle)
VALUES
(1),
(PI() / 2),
(PI() / 6);
Code language: SQL (Structured Query Language) (sql)
Third, retrieve data from the angles
table:
SELECT
id,
angle
FROM
angles;
Code language: SQL (Structured Query Language) (sql)
Output:
id | angle
---+-------
1 | 1.00
2 | 1.57
3 | 0.52
(3 rows)
Code language: SQL (Structured Query Language) (sql)
Finally, calculate the sines of angles from the angles table:
SELECT
id,
SINE (angle) sine_value
FROM
angles;
Output:
id | sine_value
---+-----------
1 | 0.84
2 | 1.0
3 | 0.5
(3 rows)
Code language: SQL (Structured Query Language) (sql)
Summary
- Use the
SIN()
function to calculate the sine of a number.