SQL Server COT() Function

Summary: in this tutorial, you will learn how to use the SQL Server COT() function to calculate the cotangent of a number.

Introduction to the SQL Server COT() function #

In SQL Server, the COT() function is a math function that returns the cotangent of a number.

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

COT(n)Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • n is the angle in radians for which you want to find the cotangent value.

The COT() function returns the cotangent of the argument n. If n is NULL, the COT() function returns NULL.

SQL Server COT() function examples #

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

1) Basic COT() function example #

The following example uses the COT() function to calculate the cotangent of 1 radian:

SELECT COT(1) cotangent_value;Code language: SQL (Structured Query Language) (sql)

Output:

cotangent_value
------------------
0.6420926159343306
Code language: SQL (Structured Query Language) (sql)

2) Using the COT() function with PI() function #

The following example uses the COT() function to find the cotangent of pi/4:

SELECT COT(PI()/4) cotangent_value;Code language: SQL (Structured Query Language) (sql)

Output:

cotangent_value
---------------
1.0Code language: SQL (Structured Query Language) (sql)

3) Using COT() 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
  (NULL),
  (PI() / 4),
  (PI() / 2);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  | NULL
2  | 0.79
3  | 1.57
(3 rows)Code language: SQL (Structured Query Language) (sql)

Finally, calculate the cotangents of angles from the angles table:

SELECT
  id,
  angle,
  ROUND(COT(angle),0) tangent_value
FROM
  angles;Code language: SQL (Structured Query Language) (sql)

Output:

id | angle | tangent_value
---+-------+--------------
1  | NULL  | NULL
2  | 0.79  | 1.0
3  | 1.57  | 0.0
(3 rows)Code language: SQL (Structured Query Language) (sql)

Summary #

  • Use the COT() function to calculate the cotangent of a number.
Was this tutorial helpful?