SQL - ATN2() Function



The SQL ATN2() function accepts two numeric values i.e. x and y as arguments and returns the arc tangent value. The domain of the argument must be (-∞, ∞) × (-∞, ∞) i.e. the set of all real numbers for both the arguments and the range of the result will be [-π, π]. If the value passed to this function doesn't lie in the given domain, it raises an error.

The value returned by this function is measured in radians between the positive x-axis and the line from the origin to the point (y,x) in the Cartesian plane, where x and y are the two arguments. The quadrant of the result depends on the signs of the arguments.

Syntax

Following is the syntax of SQL ATN2() function −

ATN2(Y,X)

where, Y and X are the specified numeric values.

Example

If we pass a negative value to the first argument and a positive value to the second argument to this method, it returns a negative value.

Here, we are passing -9 and 8 as Y and X respectively.

SELECT ATN2(-9, 8) 
AS arc_tangent2

When we run above program, it produces following result −

+--------------------+
| arc_tangent2       |
+--------------------+
| -0.844153986113171 |
+--------------------+

Example

If we pass a positive value to the first argument and a negative value to the second argument to this method, it returns a positive value.

Here, we are passing 10 and -12 as Y and X respectively.

SELECT ATN2(10, -12) 
AS arc_tangent2

Following is an output of the above code −

+-------------------+
| arc_tangent2      |
+-------------------+
| 2.44685437739309  |
+-------------------+

Example

We can also pass the mathematical constant PI as an argument to this function, it returns it's equivalent atn2 value.

In here, we are trying to pass pi() function as arguments to the atn2() function as shown below −

SELECT ATN2(pi(), pi())        
AS arc_tangent2

While executing the above code we get the following output −

+--------------------+
| arc_tangent2       |
+--------------------+
| 0.785398163397448  |
+--------------------+

Example

In here, we are trying to pass an expression '8.3 + 0.87', '76' as an argument to the function

SELECT ATN2(8.3 + 0.87, 76) 
AS arc_tangent2

Output of the above code is as follows −

+--------------------+
| arc_tangent2       |
+--------------------+
| 0.120077430993549  |
+--------------------+

Example

Assume we have created a table with name CUSTOMERS as shown below −

create table CUSTOMERS(ID INT NOT NULL, 
NAME VARCHAR(20) NOT NULL, 
AGE INT NOT NULL, 
ADDRESS CHAR(25), 
SALARY DECIMAL(18, 2), 
PRIMARY KEY(ID));
Commands completed successfully.

Let us insert r values into it −

insert INTO CUSTOMERS VALUES(1, 'Ramesh', 32, 'Ahmedabad', 2000.00);
insert INTO CUSTOMERS VALUES(2, 'Khilan', 25, 'Delhi', 1500.00);
insert INTO CUSTOMERS VALUES(3, 'kaushik', 23, 'Kota', 2000.00);
insert INTO CUSTOMERS VALUES(4, 'Chaitali', 25, 'Mumbai', 6500.00);
insert INTO CUSTOMERS VALUES(5, 'Hardik', 27, 'Bhopal', 8500.00);
insert INTO CUSTOMERS VALUES(6, 'Komal', 22, 'MP', 4500.00);
insert INTO CUSTOMERS VALUES(7, 'Muffy', 24, 'Indore', 10000.00);

Following query calculates the atn2 value of the age and salary of all the customers −

SELECT NAME, AGE, SALARY,
ATN2(AGE, SALARY) 
AS arc_salarytn2
FROM customers;

The result produced is as follows −

+----------+-----+----------+-----------------------+
| NAME     | AGE | SALARY   | arc_salarytn2         |
+----------+-----+----------+-----------------------+
| Ramesh   |  32 |  2000.00 |  0.0159986348763435   |
| Khilan   |  25 |  1500.00 |  0.0166651237139407   |
| kaushik  |  23 |  2000.00 |  0.01149949308189     |
| Chaitali |  25 |  6500.00 |  0.00384613488106652  |
| Hardik   |  27 |  8500.00 |  0.0031764599048072   |
| Komal    |  22 |  4500.00 |  0.00488884993928742  |
| Muffy    |  24 | 10000.00 |  0.00239999539201593  |
+----------+-----+----------+-----------------------+
sql-numeric-functions.htm
Advertisements