Oracle SQL Developer
Oracle SQL Developer
Oracle SQL Developer
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Using Pseudocolumns, Sequences, and <strong>SQL</strong> Functions■■■TO_TIMESTAMP, which converts character data to a value of TIMESTAMP datatypeTO_TIMESTAMP_TZ, which converts character data to a value of TIMESTAMPWITH TIME ZONE data typeTO_YMINTERVAL, which converts a character string to an INTERVAL YEAR TOMONTH typeExample 6–18 shows how to use date functions on date data.Example 6–18Using Date Functions-- in the following statement you can use MONTHS_BETWEEN to compute months-- employed for employees and then truncate the results to the whole month-- note the use of the label (alias) "Months Employed" for the computed columnSELECT employee_id, TRUNC(MONTHS_BETWEEN(SYSDATE, HIRE_DATE)) "Months Employed"FROM employees;-- the following displays the year hired for each employee idSELECT employee_id, EXTRACT(YEAR FROM hire_date) "Year Hired" FROM employees;Example 6–19 shows how to use date functions with format masks.Example 6–19Using Date Functions With Format Masks-- use TO_DATE with a format mask to display or enter dates differently than the-- current default date format-- the following displays 1998 with the 'DD-MON-RR' format maskSELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-RR') ,'YYYY') "Year" FROM DUAL;-- note that 'YY' in a format mask denotes the year in the current century-- the following displays 2098 with the 'DD-MON-YY' format maskSELECT TO_CHAR(TO_DATE('27-OCT-98', 'DD-MON-YY') ,'YYYY') "Year" FROM DUAL;-- the following displays the date and time with a datetime format maskSELECT TO_TIMESTAMP ('10-Sep-05 14:10:10.123000', 'DD-Mon-RR HH24:MI:SS.FF')FROM DUAL;-- the following displays the system date and time with a format maskSELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') "Now" FROM DUAL;6.5.7 Using Aggregate FunctionsGroup functions operate on sets of rows to give one result per group. These sets maycomprise the entire table or the table split into groups.Example 6–18 shows how to use aggregate functions on collections of data in thedatabase. Aggregate functions include COUNT, MAX, MIN, and SUM. The GROUP BYclause is used to select groups of rows by a specified expression and returns one rowof summary information for each group.Example 6–20 Using Aggregate Functions-- you can use COUNT to count the employees with manager 122-- note the use of a column alias Employee CountSELECT COUNT(*) "Employee Count" FROM employees WHERE manager_id = 122;-- count the employees grouped by manager, also sort the groupsSELECT COUNT(*) "Employee Count", manager_id FROM employeesGROUP BY manager_id ORDER BY manager_id;6-14 <strong>SQL</strong> <strong>Developer</strong> Online Help