// math reference

SQL ATAN2 function

Returns the angle between the positive x-axis and the point (x, y), in radians, using the signs of both arguments to pick the quadrant.

类别
Math
Returns
float / double (radians)
In this category
37 of 82

语法

ATAN2(y, x)

引擎支持

引擎Support备注
SQL Server Different name or syntax No ATAN2 name ('not a recognized built-in function'); ATN2(y, x) does the same job
PostgreSQL 是 Supported under this name.
MySQL 是 ATAN2(y, x), also spelled ATAN(y, x)
SQLite 是 Requires SQLite 3.35+ with the math functions built in
Oracle 是 Supported under this name.

示例

PostgreSQL

SELECT ATAN2(1, 1) AS quarter_pi;  -- 0.7853981633974483

SQL Server

SELECT ATN2(1, 1) AS quarter_pi;  -- same value, different name

PostgreSQL

SELECT DEGREES(ATAN2(delta_y, delta_x)) AS bearing
FROM vectors;

值得了解

  • Arguments are (y, x) in that order on all five engines, SQL Server's ATN2(y, x) included. Swapping them silently returns PI()/2 minus the intended angle (for positive x and y) rather than erroring.
  • ATAN2(1, 1) is PI()/4 but ATAN2(-1, 1) is -PI()/4: the sign of y picks the lower half of the circle, which single-argument ATAN cannot express.
  • ATAN2(0, 0) has no meaningful angle. PostgreSQL, MySQL and SQLite return 0, while SQL Server's ATN2(0, 0) raises an invalid floating point operation and Oracle raises ORA-01426. Filter out the origin first.

Questions about ATAN2

What is the difference between ATAN and ATAN2?

ATAN(y/x) loses the quadrant because y/x collapses two numbers into one. ATAN2(y, x) keeps both signs and returns the angle in (-pi, pi], the right one for bearings and vectors.

What is the argument order of ATAN2?

Y first, X second: ATAN2(y, x) on MySQL, PostgreSQL, SQLite, Oracle, and on SQL Server, where the function is spelled ATN2.

Why is ATAN2 called ATN2 on SQL Server?

Historical T-SQL naming. It computes the same thing with the same (y, x) order; SQL Server has no ATAN2 alias, so a cross-engine query has to swap the name.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.