Idbslab 09
Idbslab 09
Experiment 09
Implementation of SQL Date and String functions and conversion functions.
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'
DESCRIPTION:
The Oracle/PLSQL TO_DATE function converts a string to a date.
SYNTAX
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:
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.
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:
The following statement returns the date of the first Tuesday following today's date.
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:
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
Parameters or Arguments
string1 is the first 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,
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:
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.
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:
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
TO_CHAR(sysdate, 'yyyy/mm/dd')
Result: '2003/07/09'
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:
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.
Task : List employee names and their HIREDATE in the following formats
D.ALI 17/12/80
SHORT QUIZ:
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.