Summary: in this tutorial, you will learn how to use the SQL Server TAN() function to calculate the tangent of a number.
Introduction to the SQL Server TAN() function #
In SQL Server, the TAN() function is a math function that returns the tangent of a number.
Here’s the syntax of the TAN() function:
TAN(n)Code language: SQL (Structured Query Language) (sql)In this syntax:
nis the angle in radians for which you want to find the tangent value.
The TAN() function returns the tangent of the argument n. If n is NULL, the TAN() function returns NULL.
SQL Server TAN() function examples #
Let’s take some examples of using the TAN() function.
1) Basic TAN() function example #
The following example uses the TAN() function to calculate the tangent of 1 radian:
SELECT TAN(1) tangent_value;Code language: SQL (Structured Query Language) (sql)Output:
tangent_value
------------------
1.5574077246549023
Code language: SQL (Structured Query Language) (sql)2) Using the TAN() function with PI() function #
The following example uses the TAN() function to find the tangent of pi/4:
SELECT ROUND(TAN(PI()/4),0) tangent_value;Code language: SQL (Structured Query Language) (sql)Output:
tangent_value
-------------
1.0
Code language: SQL (Structured Query Language) (sql)3) Using TAN() 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
(0),
(PI() / 4),
(PI());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 tangents of angles from the angles table:
SELECT
id,
angle,
ROUND(TAN(angle), 0) tangent_value
FROM
angles;Code language: SQL (Structured Query Language) (sql)Output:
id | angle | tangent_value
---+-------+--------------
1 | 0.00 | 0.0
2 | 0.79 | 1.0
3 | 3.14 | 0.0
(3 rows)Code language: SQL (Structured Query Language) (sql)Summary #
- Use the
TAN()function to calculate the tangent of a number.