Atn2 Function In Sql in SQL
? ATN2 (or ATAN2) Function in SQL
The ATN2() function (called ATAN2() in most SQL dialects) returns the arctangent of two numbers — typically y and x coordinates — and computes the angle between the positive x-axis and the point (x, y) on a plane.
? Purpose
Calculates the angle (in radians) from the origin to the point
(x, y).Takes into account the signs of both inputs to determine the correct quadrant.
Returns a value between -? and ?.
? Syntax
ATN2(y, x)or in some databases:
ATAN2(y, x)y: The y-coordinate (numerator)x: The x-coordinate (denominator)
? Examples
1. Basic usage:
SELECT ATN2(1, 1) AS angle_radians;-- Returns approx 0.785398 (?/4 radians)2. Angle in degrees:
SELECT ATN2(1, 1) * 180 / PI() AS angle_degrees;-- Returns approx 45 degrees3. Different quadrant:
SELECT ATN2(-1, -1) * 180 / PI() AS angle_degrees;-- Returns approx -135 degrees (or 225 degrees if normalized)? Use Cases
Calculating direction or heading from coordinates.
Used in GIS (Geographic Information Systems) or spatial computations.
Computing angles when both
xandycomponents are known.
?? Supported In
SQL Server:
ATN2(y, x)MySQL, PostgreSQL, Oracle: use
ATAN2(y, x)
If you want, I can also show you how to use it with coordinate data or combine with other trigonometric functions!