SQL Server ACOS() Function
ACOS() returns the angle in radians whose cosine is the specified value. The input must be between -1 and 1; values outside the range cause a domain error. The input must be a float or a type SQL Server can implicitly convert to float, and the return type is float. See Microsoft’s ACOS() reference.
Syntax
ACOS(float_expression)
float_expression is the cosine value whose arccosine is returned.
Usage
The ACOS() function is commonly used to calculate inverse cosine values, and can be used in trigonometric-related calculations. For example, when calculating the angle between two vectors, this function can be used to calculate the quotient of the dot product and the product of their magnitudes, and then convert it to degrees.
Examples
Here are two examples of using the ACOS() function:
Example 1: Calculate the inverse cosine value of a given number
SELECT ACOS(0.5) AS angle_radians;
Result:
| angle_radians |
|---|
| 1.0471975511966 |
Example 2: Calculate the angle between two vectors using the ACOS() function
For two vectors with a dot product of 2 and magnitudes of 5 and 4, use floating-point inputs so division retains its fractional value. ACOS() returns radians; wrap it with DEGREES() for a degree result:
DECLARE @dot_product float = 2;
DECLARE @magnitude_a float = 5;
DECLARE @magnitude_b float = 4;
SELECT DEGREES(ACOS(@dot_product / (@magnitude_a * @magnitude_b))) AS angle_degrees;
Result (rounded for display):
| angle_degrees |
|---|
| 84.2608 |
Conclusion
ACOS() expects a cosine value and returns radians. The input must be in the interval [-1, 1]; use DEGREES() only when a degree-valued result is required.