Beruflich Dokumente
Kultur Dokumente
Page 1 of 11
After completing this topic, you should be able to describe and use character, number, and date functions in SELECT statements in an Oracle9i database.
format data convert datatypes perform calculations on data modify data items manipulate output
Most SQL functions accept arguments data or expressions that you supply as input. They then process the arguments to determine a result value. The diagram displayed shows the way in which arguments and result values relate to a function. Based on the kind of data they manipulate, SQL functions fall into two categories:
l l
single-row functions Single-row functions manipulate data from a single row and return a single result value per row. They include character, number, date, and conversion functions. multiple-row functions Multiple-row functions manipulate data from multiple rows and return a single result value for the entire group of rows. These functions include statistical functions that evaluate sums, averages, and standard deviations.
Single-row functions can accept multiple arguments of differing datatypes and can return result values of a datatype that differs from those of the arguments. Each argument can be a constant, a variable, a column name, or an expression. When you use an expression as an argument, you can include other functions within the expression in a process known as nesting. You can use single-row functions in SELECT, WHERE, and ORDER BY clauses.
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 2 of 11
character functions number functions date functions conversion functions general functions
character functions Character functions accept input in character format. They return result values either as character strings or as numbers. number functions Number functions accept, process, and return numeric values. date functions Date functions process values of the date datatype. They all accept dates as arguments and return either date or numeric values. conversion functions Conversion functions convert values from one datatype into another datatype. general functions General functions include functions that handle null values and functions that allow you to formulate conditional statements.
2. Character functions
Character functions can manipulate the case uppercase or lowercase of characters or modify the characters themselves. Functions for manipulating case are LOWER, UPPER, and INITCAP. The LOWER function converts input characters into lowercase.
The INITCAP function converts the first character of an input string from lowercase to uppercase and the remaining characters in the string to lower case.
LENGTH
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 3 of 11
CONCAT
SUBSTR
INSTR
TRIM
REPLACE
The SUBSTR function extracts part of the input string, beginning at a specified position in the string and continuing for a specified number of characters. If the position specified in a SUBSTR function is a negative number, Oracle locates the position starting from the end of the string.
The INSTR function searches for a specified string within an input string and returns its numeric position. Optionally, you can specify a position for the start of the search.
The LPAD function pads an input string to a specified length by appending a specified character or string to the beginning of the input string. The RPAD function appends the padding characters to the end of an input string.
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 4 of 11
The TRIM function removes all occurrences of a specified character from the beginning or the end or both of an input string.
The REPLACE function searches an input string for a specified search string and replaces all occurrences of the search string with a specified replacement string.
You can combine character-manipulation and case-manipulation functions in a single SQL statement. This allows you to format query output to suit your requirements.
SELECT employee_id, UPPER(CONCAT(SUBSTR(last_name, 1, 3), SUBSTR(first_name, 1, 3))), INITCAP(last_name), INSTR(employee_id, '1') FROM employees;
In the example displayed, the UPPER, INITCAP, CONCAT, SUBSTR, and INSTR functions in a single SQL statement will produce formatted output of employee data.
SELECT employee_id, UPPER(CONCAT(SUBSTR(last_name, 1, 3), SUBSTR(first_name, 1, 3))), INITCAP(last_name), INSTR(employee_id, '1') FROM employees;
This statement returns each user's employee ID, a combination of the first three letters of each user's last and first names, a formatted version of the last name, and the position of the number 1 in each employee ID.
Question
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 5 of 11
1. 2. 3. 4.
Answer
You use SUBSTR to extract part of a string. Option 1 is incorrect. The INSTR function searches for a specified string within an input string and returns its numeric position. You can optionally specify a position within the input string to start the search. Option 2 is incorrect. The RPAD function pads an input string to a specified length by appending a specified character or string to the end of the input string. You might use RPAD to append trailing spaces to an address string, for example. Option 3 is correct. The SUBSTR function extracts part of the input string, beginning at a specified position in the string and continuing for a specified number of characters. If the position specified in a SUBSTR function is a negative number, Oracle locates the position starting from the end of the string. Option 4 is incorrect. The TRIM function removes all occurrences of a specified character from the beginning or the end or both of an input string. You might use TRIM to remove trailing spaces, for example.
3. Number functions
SQL supports the following number functions for modifying the numeric values returned in results:
l
ROUND
TRUNC
MOD
The TRUNC function truncates a number to a specified number of decimal places without rounding the number.
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 6 of 11
The MOD function divides a number by another number and returns the remainder of the division. This function is useful for finding out whether a value is odd or even.
Oracle includes a dummy table called DUAL. The DUAL table contains only one column and one row, which contains the value X. The DUAL table allows you to complete the compulsory FROM clause of SELECT statements in cases in which you don't need to query a table for user data. You often use the DUAL table when performing calculations that use number functions. In the example displayed, the ROUND function includes a literal number rather than a table reference as input and so the SELECT statement does not need to query any tables. You therefore use the DUAL table to complete the FROM clause.
Note
You may choose to perform numeric calculations in this way so that you can insert the resulting data into a table or include the calculations as part of conditional clauses.
When you use the ROUND function, you can specify the number of decimal places for the return value. If you specify 0 or leave out this argument, the function rounds the input value to the nearest integer.
You can specify a negative number of decimal places in a ROUND function. In the example displayed, the ROUND function will round the input value up to the nearest hundred.
When you specify the number of decimal places for the TRUNC function, you have the same options as for the ROUND function. In the example displayed the TRUNC function rounds the value down to the nearest hundred.
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 7 of 11
Question
What is the result of the following SELECT statement? SELECT TRUNC (214.52, -1) FROM DUAL; Options: 1. 210 2. 214 3. 214.5
Answer
The TRUNC function truncates the input value to 210. Option 1 is correct. The 1 argument truncates (makes zero) one digit to the left of the decimal point. That is, it rounds down the input value to the nearest "tens" value. Option 2 is incorrect. If the decimal places value is 0, or omitted, the input value would be truncated to an integer. Option 3 is incorrect. A decimal places argument value of 1 yields a result of 214.5 . However, negative values truncate to the left of the decimal point.
4. Date functions
Oracle's default input and display format for dates includes the day, a three-character abbreviation of the month, and the last two digits of the year. The statement displayed, for example, will retrieve the hire date of an employee in the default format.
The date values that Oracle stores may contain full date and time information, including century, year, month, day, hour, minute, and second. The statement example displayed retrieves all information rather than only the default information about the hire date of the employee.
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 8 of 11
SELECT TO_CHAR(hire_date, 'DY, DD MONTH YYYY HH:MI:SS') FROM employees WHERE last_name = 'Austin';
Note
Because it includes century information, the Oracle date format is Y2K compliant.
You use the SYSDATE function to obtain the current date and time. Generally, you obtain this information by querying the dummy table called DUAL because the date information is obtained in real time rather than from a table.
You can use the following SQL functions with date input:
l
NEXT_DAY
LAST_DAY
MONTHS_BETWEEN
ADD_MONTHS
The NEXT_DAY function returns the date of a specific day of the week that follows the input date. You can specify the day of the week by name or number. For example, you can retrieve the date of the next Friday after a certain date by specifying 'Friday' or 5.
The LAST_DAY function returns the date of the last day of the month that includes the input date.
The MONTHS_BETWEEN function returns the time interval between two input dates, expressed as months and decimal points of months.
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 9 of 11
The ADD_MONTHS function adds a specified number of calendar months to the input date. You can subtract months from an input date by specifying a negative value.
Question
Which operations do you think you can perform on Oracle date values? Options: 1. 2. 3. 4. Rounding Truncation Addition of two dates Subtraction of one date from another date
Answer
You can subtract one date from another date and round and truncate dates, but you cannot add dates to each other.
You can add and subtract a specific number of days to and from dates. In the example displayed, the number of days added to a date in the employees table is five.
You can subtract one date from another date to determine the number of days between them. In the example displayed, the statement subtracts the current date from the last day of the current month to retrieve the number of days left in the current month.
To round or truncate date values, you specify the date element week or month, for example to which you want the date to be rounded or truncated. The default date
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 10 of 11
You can round a date to the nearest week and truncate a date to the nearest month.
Question
Identify the true statements about Oracle's data functions. Options: 1. You can use the ADD_MONTHS function to subtract months from a date 2. You can use the MONTHS_BETWEEN function to return an integer number of months between two dates 3. You can use the LAST_DAY function to determine the date that precedes an input date 4. You can use the SYSDATE function to obtain the current time
Answer
You can use the ADD_MONTHS function to subtract months from a date, and you can use the SYSDATE function to obtain the current time. Option 1 is correct. You use ADD_MONTHS to add or subtract a specified number of calendar months from an input date. If the resulting month has fewer days than the day component of the input date, then the result is the last day of the resulting month. Option 2 is incorrect. The MONTHS_BETWEEN function calculates a fractional result based on a 31-day month. You can use TRUNC on the result to retrieve an integer. Option 3 is incorrect. The LAST_DAY function returns the date of the last day of the month that includes the input date. Option 4 is correct. You use the SYSDATE function to obtain the current date and time. You typically obtain this information by querying the DUAL table.
Summary
SQL supports functions that perform calculations or modifications on input values and
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007
Page 11 of 11
return result values. Single-row functions perform operations on one row at a time, whereas multiple-row functions perform operations on groups of rows to return a single result value. Character functions manipulate character values. You can use character functions to change the case of characters and to search for, modify, or extract characters from character strings. Number functions manipulate numeric values. You can use number functions to round and truncate numbers and to determine the remainder of a division expression. Although Oracle displays only day, month, and year information in dates by default, it stores dates in a format that includes time and century information. You can manipulate date values using date functions, number functions, and arithmetical expressions.
Table of Contents
| Top of page | | Learning objective | | 1. Types of SQL functions | | 2. Character functions | | 3. Number functions | | 4. Date functions | | Summary |
Copyright 2003 SkillSoft. All rights reserved. SkillSoft and the SkillSoft logo are trademarks or registered trademarks of SkillSoft in the United States and certain other countries. All other logos or trademarks are the property of their respective owners.
http://xlibrary.skillport.com/courseware/cbtlib/66472/68129/eng/thin/transcript.html
5/11/2007