BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

SQL Server
SQL 2005+

SQL Server Trigonometric Functions

The forty-first part of the SQL Server Programming Fundamentals tutorial continues the examination of Transact-SQL (T-SQL) mathematical functions. This article considers the trigonometric functions that allow SQL Server to work with angular data.

Arcsine

Arcsine is the inverse function of sine. Where the sine of an angle returns the ratio of the lengths of the opposite side and the hypotenuse, the arcsine of that ratio returns the angle. In T-SQL, we can demonstrate this by applying the Asin function to the result of a Sin operation. As you can see from the sample below, the original angle is returned. The angles are show in radians in this case:

print sin(0.6433)       -- 0.599839
print asin(0.599839)    -- 0.6433

To convert an angle from radians to degrees, you can use the Degrees function. We can use a combination of the Radians and Degrees functions to repeat the above example:

print sin(radians(36.86))       -- 0.599862
print degrees(asin(0.599862))   // 36.86

Arccosine

Arccosine is the inverse of cosine. Given the ratio of the adjacent side and the hypotenuse of a right-angled triangle, arccosine returns the angle θ. In the diagram this ratio is 0.8 (4m / 5m). We can calculate the angle from this ratio using T-SQL's Acos function.

print degrees(acos(0.8))    -- 36.8699

Arctangent

The inverse of tangent is known as arctangent. Accessible using the Atan function, arctangent calculates an angle based upon the ratio of the opposite and adjacent sides. For the example triangle, this ratio is 0.75.

print degrees(atan(0.75))   -- 36.8699

The last of the trigonometric functions that T-SQL provides is Atn2. This variation upon Atan allows the opposite and adjacent side lengths to be provided using two arguments. The arctangent of the ratio of the two lengths is returned.

print degrees(atn2(3, 4))   -- 36.8699

Pi

Although not technically a trigonometric function, T-SQL provides a function named Pi. This returns a constant value that approximates Pi (π), which is the ratio between a circle's circumference and its diameter. The Pi functions returns a value that is rounded to sixteen decimal places.

print convert(numeric(17,16), pi()))    -- 3.1415926535897931
3 November 2009