0% found this document useful (0 votes)
10 views

DBMS Lab 3

The document discusses different SQL functions categorized into number, aggregate, character, and date functions. It provides examples of functions like ABS(), AVG(), CEILING(), COUNT(), FLOOR(), MAX(), MIN(), ROUND(), SIGN(), SUM(), LOWER(), UPPER(), TRIM(), TO_CHAR(), TO_NUMBER(), TO_DATE(), NOW(), CURDATE(), CURTIME(), DATE(), EXTRACT(), DATE_ADD(), DATE_SUB(), DATEDIFF(), and DATE_FORMAT() along with explanations and code samples.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DBMS Lab 3

The document discusses different SQL functions categorized into number, aggregate, character, and date functions. It provides examples of functions like ABS(), AVG(), CEILING(), COUNT(), FLOOR(), MAX(), MIN(), ROUND(), SIGN(), SUM(), LOWER(), UPPER(), TRIM(), TO_CHAR(), TO_NUMBER(), TO_DATE(), NOW(), CURDATE(), CURTIME(), DATE(), EXTRACT(), DATE_ADD(), DATE_SUB(), DATEDIFF(), and DATE_FORMAT() along with explanations and code samples.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment_3

Implementation of different types of function with suitable examples


• Number function
• Aggregate Function
• Character Function
• Conversion Function
• Date Function

Q . 1 Execute all below functions on sample number, string and date values .

Function Description
ABS Returns the absolute value of a number.
AVG Returns the average value of an expression/column values.
CEILING Returns the nearest integer value which is larger than or equal to the specified decimal
value.
COUNT Returns the number of records in the SELECT query.
FLOOR Returns the largest integer value that is less than or equal to a number. The return value is
of the same data type as the input parameter.
MAX Returns the maximum value in an expression.
MIN Returns the minimum value in an expression.
RAND Returns a random floating point value using an optional seed value.
ROUND Returns a numeric expression rounded to a specified number of places right of the
decimal point.
SIGN Returns an indicator of the sign of the input integer expression.
SUM Returns the sum of all the values or only the distinct values, in the expression. NULL
values are ignored.
SQL Numeric Functions Aggregate Function

Example: ABS()

SELECT ABS(-7 + 5) AS Result

In the following example, the AVG() is used with the Salary column of the Employee table. It
calculates the average of all salaries and the average of distinct salaries.
Example: AVG()

SELECT AVG ( Salary) AS AllSalary, FROM Employee

SELECT CEILING(23.34) AS PosInt, CEILING (-23.34) AS NegInt


o/p

PosInt =23
NegInt=-23

COUNT() Function

Parameters
ALL: Applies the aggregate function to all the values in the group. All values are counted.
This is the default value.
DISTINCT: Applies the aggregate function to only distinct not null values.
expression: An expression of any type except text, ntext, or image.
The * specifies that the COUNT() function should consider all rows to arrive at the total table
rows count.
• COUNT(*) returns the number of rows in a table. This includes duplicates and null
values.
• COUNT(*) does not take any other parameter and does not support DISTINCT.
• COUNT(*) does not need an expression as it does not use any information about any
particular column.
In this simple example, the COUNT(*) returns the total number of rows in the Employee table.
Example: COUNT(*)

SELECT COUNT(*) AS EmpCount FROM Employee;

SELECT FLOOR(0.01) AS PosInt, FLOOR(0.00) AS Result;

SELECT MAX(Salary) AS MaxSal FROM Employee;

SELECT MIN(Salary) AS MinSal FROM Employee;

SELECT ROUND (123.456, 0) AS Round0,


ROUND (123.456, 1) AS Round1,
ROUND (123.456, 2) AS Round2

The SIGN() function returns -1, 0, 1 based on the specified numeric value. It returns -1 for
any negative number, -1.0 for negative decimal, 1.0 for positive decimal, 1 for positive
integer, and 0 for 0 value.

SELECT SIGN (-234) AS SignNeg, SIGN (-0.45) AS DecNeg, SIGN (0) AS SignZero,
SIGN (0.45) AS DecPos, SIGN (234) AS SignPos
SELECT SUM(SALARY) FROM Employee;

SQL c haracter function


A character or string function is a function which takes one or more characters or numbers as
parameters and returns a character value. Basic string functions offer a number of capabilities
and return a string value as a result set.

Functions Description

lower() The SQL LOWER() function is used to convert all characters of a string to
lower case.

upper() The SQL UPPER() function is used to convert all characters of a string to
uppercase.

trim() The SQL TRIM() removes leading and trailing characters(or both) from a
character string.

LOWER(string)
UPPER(string)

TO_CHAR(number,format,parameters)

SELECT CHAR(sysdate, “Month DD,YYYY”) FROM DUAL;


This returns the system date in the form of 31 July, 2018

TO_NUMBER (‘353.78’);
TO_DATE
This function takes character values and returns the output in the date format.
The syntax of this function is given as follows −

TO_DATE(number, format, parameters)


This function changes the given string to number with the specific format as provided according
to the syntax.

SELECT TO_DATE(‘2018/07/31’,’yyyy/mm/dd’) FROM DUAL;


This takes the values in character format and returns them in date format as specified.
MySQL Date Functions
The following table lists the most important built-in date functions in MySQL:
Function Description
NOW() Returns the current date and time
CURDATE() Returns the current date
CURTIME() Returns the current time
DATE() Extracts the date part of a date or date/time expression
EXTRACT() Returns a single part of a date/time
DATE_ADD() Adds a specified time interval to a date
DATE_SUB() Subtracts a specified time interval from a date
DATEDIFF() Returns the number of days between two dates
DATE_FORMAT() Displays date/time data in different formats

You might also like