Exercise
Table: Charity
I. Display all first names in lowercase.
II. Display all last names of people of Mumbai city in uppercase .
Exercise
Table: Charity
III. Display Person Id along with First 3 characters of his/her name.
IV. Display first name concatenated with last name for all the employees.
V. Display length of address along with Person Id
VI. Display last 2 characters of City and Person ID.
VII. Display Last Names and First names of people who have "at" in the second or third position in their first names.
VIII. Display the position of 'a' in Last name in every row.
IX. Display Last Name and First name of people who have "a" as the last character in their First names.
X. Display the first name and last name concatenated after removing the leading and trailing blanks. Display Person Id,
last names and contribution rounded to the nearest rupee of all the persons.
XI. Display Person Id, last name and contribution with decimal digits truncated of all the persons.
XII. Display Last name, contribution and a third column which has contribution divided by 10. Round it to two decimal
points.
MySQL
Functions
Recap
• Math Functions:
Pow()/Power( )
Round( )
Mod( )
Types of Function
• Single Row Functions: Single row functions operate on a single value to return a single value. They can
accept one or more arguments but return only one result per row. When applied on a table, they return
a single result for every row of the queried table. They are further categorized into:
String/Text functions
Math functions
Date and Time functions
• Multiple Row/Aggregate Functions: Multiple row functions operate on a set of rows to return a
single value. Examples include SUM(), AVG() , MAX(), MIN() and COUNT().
Date & Time Functions
• Date and Time functions allow us to perform many types of tasks on Date
type data. The default date format in MySQL is YYYY-MM-DD.
Date and Time Functions are as follows:
NOW (), DATE (), MONTH (), MONTHNAME (), YEAR (), DAY (), DAYNAME ()
Table: Employee
Date and Time Functions…
• NOW( ):
Returns the current date and time in 'YYYY-MM-DD HH:MM:SS' or
YYYYMMDDHHM [Link] format, depending on whether the function is used in
a string or numeric context.
For example:
mysql> SELECT NOW();
Result: 2020-04-09 [Link]
Date and Time Functions…
• DATE( expr ):
Extracts the date part of a date or datetime expression
For example:
mysql> SELECT DATE('2010-02-26 [Link]’);
Result: '2010-02-26’
Date and Time Functions…
• MONTH( expr ):
Returns the numeric month from the date passed, in the range 0 to 12. It returns 0 for dates
such as '000000-00' or '2010-00-00' that have a zero month part.
For example:
mysql> SELECT MONTH('2010-02-26’);
Result: 2
mysql> select id, date_join, month(date_join) from employee;
Result: ->
Date and Time Functions…
• MONTHNAME( expr ):
Return the name of the month for a date
For example:
mysql> SELECT MONTHNAME('2010-02-26’);
Result: February
mysql> SELECT MONTHNAME('2010-11-12’);
Result: November
Date and Time Functions…
• YEAR( expr ):
Returns the year for date passed in the range 0 to 9999. Returns values like 1998, 2010,1996
and so on.
For example:
mysql> SELECT YEAR('2010-02-26’);
Result: 2010
mysql> SELECT id, date, join, year (date_join) from employee;
Result: ->
Date and Time Functions…
• DAY( expr ):
Returns the day of the month for a specified date. The day returned will be within the range of
1 to 31. If the given date is '0000-00-00', the function will return 0. The DAYOFMONTH() is the
synonym of DAY().
For example:
mysql> SELECT DAY('2010-02-26’);
Result: 26
Date and Time Functions…
• DAYNAME( expr ):
Returns the name of the weekday for the date passed.
For example:
mysql> SELECT DAYNAME(‘2020-04-09’);
Result: Thursday
mysql> Select id, date_join, dayname (date_join) from employee;
Result: ->
Exercise
1. Which Date function displays the result like "Monday" or
"Tuesday" etc.
2. Name a
i. date function that returns a number.
ii. String function that returns a number.
iii. date function that returns a date.
3. Write SQL statements to do the following:
i. Display the name of current month.
ii. Display the date 10 years from now. Label the column "Future."
iii. Display the day name of week on which your birthday will fall or fell in
2010.
Exercise
4. Write the output that the following statements will produce:
i. SELECT ROUND(745678.3456, -2);
ii. SELECT MOD(3,3);
iii. SELECT DAYOFMONTH('2009-08-25’);
iv. SELECT MONTH('2010-02-26’);
v. SELECT RIGHT('Informatics', 4);
vi. SELECT LENGTH(MID(‘COMPUTER’,4,4));
[Link] LENGTH(‘ MY BOOK ‘);
[Link] YEAR(‘2010-09-08’);
ix. SELECT UPPER(mid(‘computer’,4));
x. SELECT LENGTH(TRIM(‘ COMPUTER ‘));