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

Using Single-Row Functions To Customize Output Ex - No: 3 Date

This document contains examples of using single-row functions in SQL queries to customize and manipulate data from a database. It includes 14 examples of queries using functions like TO_CHAR, INITCAP, LENGTH, ROUND, LPAD, RPAD, NVL, and DECODE to format dates, calculate values, handle nulls, and classify data into grades based on job IDs. The goal is to demonstrate how to use these functions to select, filter, and present data in a readable format for different reporting needs.

Uploaded by

Anand Kumar VP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Using Single-Row Functions To Customize Output Ex - No: 3 Date

This document contains examples of using single-row functions in SQL queries to customize and manipulate data from a database. It includes 14 examples of queries using functions like TO_CHAR, INITCAP, LENGTH, ROUND, LPAD, RPAD, NVL, and DECODE to format dates, calculate values, handle nulls, and classify data into grades based on job IDs. The goal is to demonstrate how to use these functions to select, filter, and present data in a readable format for different reporting needs.

Uploaded by

Anand Kumar VP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

Using Single-Row Functions to Customize Output

Ex.no: 3

Date:

Aim:

To Use character, number, date, general and conversion functions in SELECT statements.

Questions

1. Write a query to display the current date. Label the column Date.

QUERY: select to_char(sysdate,'dd-mon-yyyy')"date" from dual;

OUTPUT:

2. The HR department needs a report to display the employee number, last name, salary, and salary
increased by 15.5% (expressed as a whole number) for each employee. Label The column New Salary.
Place your SQL statement in a text file named lab_03_02.sql.

QUERY: select employee_id,last_name,salary+salary*(15.5/100) as new_salary from hr.employees;

OUTPUT:

16
18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

3. Run your query in the lab_03_02.sql file.

QUERY: select employee_id,last_name,salary+salary*(15.5/100) as new_salary from hr.employees;

OUTPUT:

4. Modify your lab_03_02.sql query to add a column that subtracts the old salary from the new salary.
Label the column Increase. Save the contents of the file as lab_03_04.sql. Run the revised query.

QUERY: select salary-(salary*(15.5/100)) as increase from hr.employees;

OUTPUT:

5. Write a query that displays the last name (with the first letter uppercase and all other letters lowercase)
and the length of the last name for all employees whose name starts with the letters J, A, or M. Give each
column an appropriate label. Sort the results by the employees’ last names.

QUERY: select INITCAP(last_name),length(last_name) from hr.employees where first_name like '%j' or


first_name like '%A' or first_name like '%M';

17
18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

OUTPUT:

6. The HR department wants to find the duration of employment for each employee. For each employee,
display the last name and calculate the number of months between today and the date on which the
employee was hired. Label the column MONTHS_WORKED. Order your results by the number of
months employed. Round the number of months up to the closest whole number.

QUERY: select last_name,round(months_between(sysdate,hire_date),0)"Months_worked" from


hr.employees;

OUTPUT:

7. Create a report that produces the following for each employee: <employee last name> earns <salary>
monthly but wants <3 times salary>.Label the column Dream Salaries.

QUERY: select last_name||' earns '||salary||' monthly '||'but wants '||salary*3 as dream_salary from
hr.employees;

18
18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

OUTPUT:

8. Create a query to display the last name and salary for all employees. Format the salary to be 15
characters long, left-padded with the “$” symbol. Label the column SALARY.

QUERY: select last_name,salary,lpad(salary,20,'$') as salary from hr.employees;

OUTPUT:

9. Display each employee’s last name, hire date, and salary review date, which is the first Monday after
six months of service. Label the column REVIEW. Format the dates to appear in the format similar to
“Monday, the Thirty-First of July, 2000.”

QUERY: select last_name,hire_date,salary,to_char(next_day(hire_date,'monday'),'fmday," the "ddspth


"of" month,yyyy') as REVIEW from hr.employees;

19
18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

OUTPUT:

10. Display the last name, hire date, and day of the week on which the employee started. Label the
column DAY. Order the results by the day of the week, starting with Monday.

QUERY:

select last_name,hire_date,to_char(hire_date,'day') as Day from hr.employees order by Day;

OUTPUT:

11. Create a query that displays the employees’ last names and commission amounts. If an employee does
not earn commission, show “No Commission.” Label the column COMM.

QUERY:

select last_name,NVL(to_char(commission_pct),'No commisison') as COMM from hr.employees where


commission_pct is null;

20
18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

OUTPUT:

12. Create a query that displays the first eight characters of the employees’ last names and indicates the
amounts of their salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in
descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.

QUERY:

select rpad(first_name,8)"EMPLOYEES",rpad(' ',salary/1000+1, '*')"SALARIES",salary from


hr.employees order by salary desc;

OUTPUT:

13. Using the DECODE function, write a query that displays the grade of all employees based on the
value of the column JOB_ID, using the following data:

Job Grade

AD_PRES

ST_MAN

IT_PROG

SA_REP

ST_CLERK

21
18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

None of the above

QUERY:

select job_id,decode(job_id,

'AD_PRES','A',

'ST_MAN', 'B',

'IT_PROG','C',

'SA_REP', 'D',

'ST_CLERK','E',

'0')GRADE from hr.employees;

OUTPUT:

22
18LE02/18EE02/18TE02/18PD05/18FD05/18SD05/18YD05/18ID05 - Database Management Systems

14. Rewrite the statement in the preceding exercise using the CASE syntax.

QUERY:

select job_id,case job_id when 'AD_PRES' THEN 'A'

when 'ST_MAN' THEN 'B'

when 'IT_PROG' THEN 'C'

when 'SA_REP' THEN 'D'

when 'ST_CLERK' THEN 'E'

ELSE '0' END GRADE

from hr.employees;

OUTPUT:

Result:

Thus, all the queries are executed successfully.

23

You might also like