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

Idbslab 09

Here are the steps to create the EMPLOYEE table and insert sample data: 1. Create the EMPLOYEE table: CREATE TABLE EMPLOYEE (EMP_ID NUMBER(5), FNAME VARCHAR2(20), LNAME VARCHAR2(25), HIREDATE DATE); 2. Insert sample data into the EMPLOYEE table: INSERT INTO EMPLOYEE VALUES(1,'John','Doe',TO_DATE('01-JAN-2015','DD-MON-YYYY')); INSERT INTO EMPLOYEE VALUES(2,'Jane','Doe',TO_DATE('15-MAR-2018','DD-MON-YYYY')); INSERT

Uploaded by

Javaria Tabassum
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Idbslab 09

Here are the steps to create the EMPLOYEE table and insert sample data: 1. Create the EMPLOYEE table: CREATE TABLE EMPLOYEE (EMP_ID NUMBER(5), FNAME VARCHAR2(20), LNAME VARCHAR2(25), HIREDATE DATE); 2. Insert sample data into the EMPLOYEE table: INSERT INTO EMPLOYEE VALUES(1,'John','Doe',TO_DATE('01-JAN-2015','DD-MON-YYYY')); INSERT INTO EMPLOYEE VALUES(2,'Jane','Doe',TO_DATE('15-MAR-2018','DD-MON-YYYY')); INSERT

Uploaded by

Javaria Tabassum
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Database Systems

Experiment 09
Implementation of SQL Date and String functions and conversion functions.

CLO-3: Use modern tools and languages.


CLO-4: Build the projects of varying complexities.
CLO-5: Demonstrate the implementation of problem solving process.
CLO-6: Work individually as well as in teams.
CLO-7: Propose a unique solution that exhibits originality of idea

Engr. Rabia Arshad


DATE datatype:

The DATE datatype stores point-in-time values (dates and times) in a table. The DATE datatype
stores the year (including the century), the month, the day, the hours, the minutes, and the
seconds (after midnight).

Oracle Database uses its own internal format to store dates. Date data is stored in fixed-length
fields of seven bytes each, corresponding to century, year, month, day, hour, minute, and second.

For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows:

'13-NOV-92'

ORACLE: TO_DATE FUNCTION

DESCRIPTION:
The Oracle/PLSQL TO_DATE function converts a string to a date.

SYNTAX

The syntax for the Oracle/PLSQL TO_DATE function is:


TO_DATE( string1, [ format_mask ])

Parameters or Arguments
string1 is the string that will be converted to a date.

format_mask is optional. This is the format that will be used to convert string1 to a date.

EXAMPLES:
TO_DATE('2003/07/09', 'yyyy/mm/dd')
Result: date value of July 9, 2003

TO_DATE('070903', 'MMDDYY')
Result: date value of July 9, 2003

TO_DATE('20020315', 'yyyymmdd')
Result: date value of Mar 15, 2002
Oracle Database stores time in 24-hour format—HH:MI:SS. By default, the time in a date field
is 00:00:00 A.M. (midnight) if no time portion is entered. In a time-only entry, the date portion
defaults to the first day of the current month. To enter the time portion of a date, use the
TO_DATE function with a format mask indicating the time portion, as in:

INSERT INTO birthdays (bname, bday) VALUES ('ANDY',TO_DATE('13-AUG-66 12:56


A.M.','DD-MON-YY HH:MI A.M.'));

NEXT_DAY:

The NEXT_DAY function returns the date of the first instance of a particular day of the week
that follows the specified date.

Return Value: DATETIME

Syntax: NEXT_DAY(datetime-expression, weekday)

Arguments :datetime-expression

An expression that has the DATETIME data type. Weekday A text expression that
identifies a day of the week (for example, Monday). Valid names are controlled by the
NLS_DATE_LANGUAGE option.

Examples:

Example 8-35 Getting a Future Date

The following statement returns the date of the first Tuesday following today's date.

SHOW NEXT_DAY(SYSDATE, 'Tues')

When today is Friday, September 8, 2000, then the following Tuesday is

11-SEP-00
ADD_MONTHS:

The ADD_MONTHS function returns the date that is n months after the specified date.

Return Value

DATETIME

Syntax

ADD_MONTHS(start_datetime, n)
Example:The following statement displays the date of the day that is one month after January
30, 2000.

SHOW ADD_MONTHS('30Jan00', 1)

Since February 29 was the last day of February 2000, ADD_MONTHS returns February 29,
2000.

29-Feb-00
Example:
Add 3 months to the current date (February 10, 2013):
SELECT ADD_MONTHS(SYSDATE, 3) FROM dual;

MONTHS_BETWEEN :

MONTHS_BETWEEN returns number of months between dates date1 and date2. If date1 is
later than date2, then the result is positive. If date1 is earlier than date2, then the result is
negative. If date1 and date2 are either the same days of the month or both last days of months,
then the result is always an integer. Otherwise Oracle Database calculates the fractional portion
of the result based on a 31-day month and considers the difference in time components date1 and
date2.

Examples:
The following example calculates the months between two dates:

SELECT MONTHS_BETWEEN
(TO_DATE('02-02-1995','MM-DD-YYYY'),
TO_DATE('01-01-1995','MM-DD-YYYY') ) "Months"
FROM DUAL;
Months
----------
1.03225806
STRING FUNCTIONS:

UPPER() or LOWER() functions


You can use either the Oracle UPPER() or LOWER() functions to format columns in your SQL
SELECT.

SELECT
UPPER(FIRSTNAME) AS "FIRSTNAME",
LASTNAME
FROM STUDENTS
WHERE RowNum < 11

CONCAT FUNCTION:
The Oracle/PLSQL CONCAT function allows you to concatenate two strings
together.

SYNTAX

The syntax for the Oracle/PLSQL CONCAT function is:


CONCAT( string1, string2 )

Parameters or Arguments
string1 is the first string to concatenate.

string2 is the second string to concatenate.

EXAMPLE
CONCAT('Tech on', ' the Net')
Result: 'Tech on the Net'

CONCAT('a', 'b')
Result: 'ab'

NOTE: Since the CONCAT function will only let you concatenate 2 strings, you will need to
nest multiple CONCAT functions to concatenate more than 2 strings together.

For example, to concatenate 3 strings, you could nest the CONCAT function as follows:
CONCAT( CONCAT( string1, string2 ), string3 )
Or you could nest the CONCAT function as follows, if you wanted to concatenate 4 strings:
CONCAT( CONCAT( CONCAT( string1, string2 ), string3 ), string4 )

String INITCAP:

INITCAP returns char, with the first letter of each word in uppercase, all other letters in
lowercase. char can be of any of the datatypes CHAR, VARCHAR2, NCHAR, or
NVARCHAR2. The return value is the same datatype as char.

Example:
SELECT INITCAP(FirstName) As "Capitals" FROM Persons;

Capitals
---------
Ola
Tove
Kari

COALESCE :

COALESCE returns the first non-null expr in the expression list. You must specify at least two
expressions. If all occurrences of expr evaluate to null, then the function returns null.

You can also use COALESCE as a variety of the CASE expression. For example,

COALESCE (expr1, expr2)

is equivalent to:

CASE WHEN expr1 IS NOT NULL THEN expr1 ELSE expr2 END

Example:
For examples, say we have the following table,

Table Contact_Info
Name Business_Phone Cell_Phone Home_Phone
Jeff 531-2531 622-7813 565-9901
Laura NULL 772-5588 312-4088
Peter NULL NULL 594-7477

and we want to find out the best way to contact each person according to the following rules:

1. If a person has a business phone, use the business phone number.

2. If a person does not have a business phone and has a cell phone, use the cell phone number.

3. If a person does not have a business phone, does not have a cell phone, and has a home phone,
use the home phone number.

We can use the COALESCE function to achieve our goal:

SELECT Name, COALESCE (Business_Phone, Cell_Phone, Home_Phone) As


Contact_Phone
FROM Contact_Info;

Result:

Name Contact_Phone
Jeff 531-2531
Laura 772-5588
Peter 594-7477
CONVERSION FUNCTIONS

ORACLE: TO_CHAR:
DESCRIPTION
The Oracle/PLSQL TO_CHAR function converts a number or date to a string.

SYNTAX
The syntax for the Oracle/PLSQL TO_CHAR function is:
TO_CHAR( value, [ format_mask ] )

Parameters or Arguments
value can either be a number or date that will be converted to a string.

format_mask is optional. This is the format that will be used to convert value to a string.

For example:

The following are number examples for the TO_CHAR function.

TO_CHAR(1210.73, '9999.9')
Result: ' 1210.7'

TO_CHAR(-1210.73, '9999.9')
Result: '-1210.7'

TO_CHAR(1210.73, '9,999.99')
Result: ' 1,210.73'

TO_CHAR(1210.73, '$9,999.00')
Result: ' $1,210.73'

TO_CHAR(21, '000099')
Result: ' 000021'
with Dates
The following is a list of valid parameters when the TO_CHAR function is used to convert a
date to a string. These parameters can be used in many combinations.

PARAMETERS EXPLANATION
YYYY 4-digit year

YEAR Year, spelled out

YYY Last 3, 2, or 1 digit(s) of year.


YY
Y

MM Month (01-12; JAN = 01).

MON Abbreviated name of month.

DD Day of month (1-31).

MONTH Name of month, padded with blanks to


length of 9 characters.

RM Roman numeral month (I-XII; JAN = I).

The following are date examples for the TO_CHAR function.

TO_CHAR(sysdate, 'yyyy/mm/dd')
Result: '2003/07/09'

TO_CHAR(sysdate, 'Month DD, YYYY')


Result: 'July 09, 2003'

TO_CHAR(sysdate, 'MON DDth, YYYY')


Result: 'JUL 09TH, 2003'
TO_CHAR(sysdate, 'Mon ddth, YYYY')
Result: 'Jul 9th, 2003'
ORACLE: TO_NUMBER FUNCTION

DESCRIPTION
The Oracle/PLSQL TO_NUMBER function converts a string to a number.

SYNTAX
The syntax for the Oracle TO_NUMBER function is:
TO_NUMBER( string1, [ format_mask] )

Parameters or Arguments
string1 is the string that will be converted to a number.

format_mask is optional. This is the format that will be used to convert string1 to a number.

EXAMPLES
TO_NUMBER('1210.73', '9999.99')
Result: 1210.73

TO_NUMBER('546', '999')
Result: 546

Since the format_mask is optional, you can simply convert a text string to a numeric value
as follows:
TO_NUMBER('1210.73')
Result: 1210.73

EXAMPLE TASK:
Display all the sections where classes start at 10:30 AM
Solution:

SELECT section_id, TO_CHAR(start_date_time, 'HH24:MI')


FROM section
WHERE TO_CHAR(start_date_time, 'HH24:MI') ='10:30'
SECTION_ID TO_CH
---------- -----
85 10:30
95 10:30
104 10:30
109 10:30
116 10:30
122 10:30

TASK: How long had each student studied in the adult literacy program?
What’s in Tasks Today…..

Task : Create table EMPLOYEE with the following attributes and then insert the following
data in to EMPLOYEE table.

EMP_ID FNAME LNAME HIREDATE ADDRESS SALARY


Perform the following tasks on the above created table:

1. List employee who have HIREDATE in JAN.


2. List employees who have HIREDATE in year 1986.
3. List employees who were hired during the 1950s.
4. List names and salary of those who joined in 86, 13 and 14.

Task : List employee names and their HIREDATE in the following formats

A.ALI 17th DEC NINETEEEN EIGHTY

B.ALI SEVENTEENTH DEC NINTEEN EIGHTY

C.ALI week day of joining

D.ALI 17/12/80

SHORT QUIZ:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7369 SMITH CLERK 7902 17-Dec-80 800 20
7499 ALLEN SALESMAN 7698 20-Feb-81 1600 300 30
7521 WARD SALESMAN 7698 22-Feb-81 1250 500 30
7566 JONES MANAGER 7839 02-Apr-81 2975 20
7654 MARTIN SALESMAN 7698 28-Sep-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-May-81 2850 30
7782 CLARK MANAGER 7839 09-Jun-81 2450 10
7788 SCOTT ANALYST 7566 09-Dec-82 3000 20
7839 KING PRESIDENT 17-Nov-81 5000 10
7844 TURNER SALESMAN 7698 08-Sep-81 1500 0 30
7876 ADAMS CLERK 7788 12-Jan-83 1100 20
7900 JAMES CLERK 7698 03-Dec-81 950 30
7902 FORD ANALYST 7566 03-Dec-81 3000 20
7934 MILLER CLERK 7782 23-Jan-82 1300 10
Q#01: The HR department wants to find the duration of employment for each
employee. For each employee, display the Emp name and calculate the number of
months between today and the date on which the employee was hired. Label the
column MONTHS_WORKED.

Q#02: List the emps who are joined in the year 81.

Q#03: List the emps who joined in any year but not belongs to the month of March.

You might also like