ABS Function in SQLThe ABS is a mathematical function in the Structured Query Language which returns the absolute value of the particular number. In simple words, this function finds the distance of a given number on the number line from zero. This function accepts single numeric or any non-numeric data and returns the data type same as the argument type. Syntax of ABS String FunctionSyntax1: This syntax uses the ABS function with the column name of the SQL table: In this SELECT syntax, we have to specify the name of that numeric column on which we want to use ABS function. Syntax2: We can also use the ABS function with the particular number as like the below syntax: Examples of ABS String functionExample 1: The following SELECT query shows the absolute value of zero: Output:
Example 2: The following SELECT query shows absolute value of the number 5: Output:
Example 3: The following SELECT query returns the position of the 'Manufacturing Company' string in the original string: Output:
Example 4: This example uses the ABS function with the table in Structured Query Language. In this example, we will create the new table through which we will run the query of ABS function on numeric columns: The following shows the syntax to create the new table in SQL: The following CREATE statement creates the Product_Details table for storing the price and quantity of products: The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table: The following SELECT statement displays the inserted records of the above Product_Details table:
Query 1: The following SELECT query uses the ABS function with the Product_ID column of the above Product_Details table: This SQL statement returns the absolute value of the Product_ID of each product. Output:
Query 2: The following SELECT query uses the ABS function with the Product_Quantity column of the above Product_Details table: This SQL statement returns the absolute value of quantity of each product. Output:
Query 3: The following SELECT query uses the ABS function with the Product_Rating column of the above Product_Details table: This SQL statement shows the absolute value of rating of each product. Output:
Query 4: The following SELECT query uses the ABS function with the Purchasing_Price and Selling_Price column of the above Product_Details table: Output:
Next TopicCOS Function in SQL
|