Using SQL Server, you can use the T-SQL ACOS()
function to return the arccosine of a number. In other words, this function returns the angle, in radians, whose cosine is the specified float expression.
The return data type is float.
You provide the number as an argument when calling the function.
Syntax
The syntax goes like this:
ACOS ( float_expression )
Where float_expression is an expression of either type float or of a type that can implicitly convert to float. Only a value ranging from -1.00 to 1.00 is valid.
Example 1 – Basic Usage
Here’s a basic example.
SELECT ACOS(0.1) Result;
Result:
+------------------+ | Result | |------------------| | 1.47062890563334 | +------------------+
Here’s what happens when you provide a value of 1
.
SELECT ACOS(1) Result;
Result:
+----------+ | Result | |----------| | 0 | +----------+
And here’s what happens when you provide a value of -1
.
SELECT ACOS(-1) Result;
Result:
+------------------+ | Result | |------------------| | 3.14159265358979 | +------------------+
Example 2 – Out-Of-Range Values
Providing a value outside the range -1
to 1
results in an error.
SELECT ACOS(2) Result;
Result:
Msg 3623, Level 16, State 1, Line 1 An invalid floating point operation occurred. Time: 0.322s
Example 3 – Expressions
You can also pass in expressions like this:
SELECT ACOS(0.1 + 0.3) Result;
Result:
+------------------+ | Result | |------------------| | 1.15927948072741 | +------------------+
Example 4 – Zero
Zero is within the accepted range.
SELECT ACOS(0) Result;
Result:
+-----------------+ | Result | |-----------------| | 1.5707963267949 | +-----------------+
Example 5 – NULL
Passing in NULL
returns NULL.
SELECT ACOS(NULL) Result;
Result:
+----------+ | Result | |----------| | NULL | +----------+