Database Programming with SQL

Similar documents
GIFT Department of Computing Science. [Spring 2016] CS-217: Database Systems. Lab-3 Manual. Single Row Functions in SQL

Database Programming - Section 1. Instructor Guide

Oracle Database SQL Basics

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Database Programming with SQL

ITEC212 Database Management Systems Laboratory 2

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Database Programming with SQL

Database Programming with SQL 5-1 Conversion Functions. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

CS Week 10 - Page 1

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

CS2 Current Technologies Lecture 2: SQL Programming Basics

SQL FUNCTIONS. Prepared By:Dr. Vipul Vekariya.

What are temporary tables? When are they useful?

Database Programming with PL/SQL

Test: Mid Term Exam Semester 2 Part 1 Review your answers, feedback, and question scores below. An asterisk (*) indica tes a correct answer.

SQL. - single row functions - Database Design ( 데이터베이스설계 ) JUNG, Ki-Hyun ( 정기현 )

Practical Workbook Database Management Systems

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.


Oracle SQL. Revision Notes

AIMMS Function Reference - Date Time Related Identifiers

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Database Programming with PL/SQL

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

SQL Structured Query Language Introduction

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Additional Practice Solutions

None of the techniques used till now allows display of data from a after some arithmetic has been done it. Computations may include displaying

Course Outline and Objectives: Database Programming with SQL

Database Programming with SQL

Built-in SQL Functions. Chapter 5

Oracle Database 12c SQL Fundamentals

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Unit 6. Scalar Functions

Getting Information from a Table

Section 1.2: What is a Function? y = 4x

Writing PL/SQL Executable Statements. Copyright 2007, Oracle. All rights reserved.

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

CS 275 Winter 2011 Problem Set 3

Data Manipulation Language

SOA IT. Fast Formula in Oracle HCM Cloud

Practical Workbook Database Management Systems

RETRIEVING DATA USING THE SQL SELECT STATEMENT

School of Computing and Information Technology. Examination Paper Autumn Session 2017

Topics Fundamentals of PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Database Programming with SQL

Introduction to Computer Science and Business

Excel Functions & Tables

The Year argument can be one to four digits between 1 and Month is a number representing the month of the year between 1 and 12.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Oracle Database: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Volume II Student Guide. D64258GC10 Edition 1.0 January 2010 D65028

3. EXCEL FORMULAS & TABLES

CSE 341 Section Handout #6 Cheat Sheet

Conversion Functions

Database Programming with SQL

Database Programming with SQL

INFORMATION TECHNOLOGY SPREADSHEETS. Part 1

SELF TEST. List the Capabilities of SQL SELECT Statements

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

SYSTEM CODE COURSE NAME DESCRIPTION SEM

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

CONCAT SUBSTR LOWER (*) All three will be evaluated simultaneously. Correct

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

Grade 4 Mathematics Pacing Guide

Introduction. Introduction to Oracle: SQL and PL/SQL

Department of Computer Science and Information Systems, College of Business and Technology, Morehead State University

CS2 Current Technologies Note 1 CS2Bh

CIMA Certificate BA Interactive Timetable

UNIT III INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL)

Payflow Implementer's Guide FAQs

This report is based on sampled data. Jun 1 Jul 6 Aug 10 Sep 14 Oct 19 Nov 23 Dec 28 Feb 1 Mar 8 Apr 12 May 17 Ju

Intermediate SQL: Aggregated Data, Joins and Set Operators

Database Design. 9-2 Basic Mapping: The Transformation Process. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

EXISTS NOT EXISTS WITH

software.sci.utah.edu (Select Visitors)

Database Programming with SQL

Using Thick Database Principles to Leverage Oracle SQL and PL/SQL Part III:

CIMA Asia. Interactive Timetable Live Online

Basic Mapping: The Transformation Process. Copyright 2011, Oracle. All rights reserved.

All King County Summary Report

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

Oracle 1Z Oracle Database 11g SQL Fundamentals I. Download Full Version :

Calendar PPF Production Cycles Non-Production Activities and Events

Database Design. 1-3 History of the Database. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Seattle (NWMLS Areas: 140, 380, 385, 390, 700, 701, 705, 710) Summary

Database Programming with PL/SQL

Designing Adhoc Reports

chapter 2 G ETTING I NFORMATION FROM A TABLE

Lecture Notes: ESC 101

Programming Languages

Transcription:

Database Programming with SQL 4-3

Objectives This lesson covers the following objectives: Demonstrate the use of SYSDATE and date functions State the implications for world businesses to be able to easily manipulate data stored in date format Demonstrate the use of SYSDATE and date functions State the implications for world businesses to be able to easily manipulate data stored in date format 3

Purpose Have you ever wondered how many days remain in the school year or how many weeks there are until graduation? Because the Oracle database stores dates as numbers, you can easily perform calculations on dates using addition, subtraction, and other mathematical operators. Businesses depend on being able to use date functions to schedule payrolls and payments, track employee performance reviews and years of service, or keep track of orders and shipments. All of these business needs are easily handled using simple SQL date functions. 4

Displaying Dates The default display and input format for dates is DD/Mon/YYYY For example: 02/Dec/2014. However, the Oracle database stores dates internally with a numeric format representing the century, year, month, day, hour, minute, and second. Valid Oracle dates are between January 1, 4712 B.C., and December 31, 9999 A.D. This represents the range of dates that you can store successfully in an Oracle database. 5

SYSDATE SYSDATE is a date function that returns the current database server date and time. Use SYSDATE to display the current date, use the DUAL table. SELECT SYSDATE FROM dual; SYSDATE 01/Jul/2015 6

DATE Data Type The DATE data type always stores year information as a fourdigit number internally: two digits for the century and two digits for the year. For example, the Oracle database stores the year as 1996 or 2004, not just as 96 or 04. In previous versions, the century component was not displayed by default. However, due to changing business requirements around the world, the 4-digit year is now the default display. 7

Working with Dates Examples: SELECT last_name, hire_date + 60 FROM employees; SELECT last_name, (SYSDATE hire_date)/7 FROM employees; SELECT employee_id, (end_date start_date)/365 AS "Tenure in last job" FROM job_history; Result Adds 60 days to hire_date. Displays the number of weeks since the employee was hired. Finds the number of days employee held a job, then divides by 365 to display in years. 8

The date functions shown in the table operate on Oracle dates. All of the date functions return a value with a DATE data type except the MONTHS_BETWEEN function, which returns a numeric data type value. MONTHS_BETWEEN ADD_MONTHS NEXT_DAY LAST_DAY ROUND TRUNC Function Number of months between two dates Add calendar months to date Date of the next occurance of day of the week specified Last day of the month Round date Truncate date Description 9

MONTHS_BETWEEN: takes 2 DATE arguments and returns the number of calendar months between the 2 dates. If the first argument is an earlier date than the second, the number returned is negative. Date Function Examples: SELECT last_name, hire_date FROM employees WHERE MONTHS_BETWEEN (SYSDATE, hire_date)>240; Result King Kochhar De Haan 17/Jun/1987 21/Sep/1989 13/Jan/1993 10

ADD_MONTHS: takes 2 arguments, a DATE and a number. Returns a DATE value with the number argument added to the month component of the date. If the number supplied is negative, the function will subtract that number of months from the date argument. Date Function Examples: SELECT ADD_MONTHS (SYSDATE, 12) AS "Next Year" FROM dual; Result 01/Jul/2016 11

NEXT_DAY: takes 2 arguments, a DATE and a weekday and returns the DATE of the next occurrence of that weekday after the DATE argument. Date Function Examples: SELECT NEXT_DAY (SYSDATE, 'Saturday') AS "Next Saturday" FROM dual; Result 04/Jul/2015 12

LAST_DAY: takes a DATE argument and returns the DATE of the last day of the month for the DATE argument. Date Function Examples: SELECT LAST_DAY (SYSDATE) AS "End of the Month" FROM dual; Result 31/Jul/2015 13

ROUND: returns a DATE rounded to the unit specified by the second argument. Date Function Examples: SELECT hire_date, ROUND (hire_date, 'Month') FROM employees WHERE department_id=50; SELECT hire_date, ROUND (hire_date, 'Year') FROM employees WHERE department_id=50; Result 16/Nov/1999 17/Oct/1995 29/Jan/1997 16/Nov/1999 17/Oct/1995 29/Jan/1997 01/Dec/1999 01/Nov/1995 01/Feb/1997 01/Jan/2000 01/Jan/1996 01/Jan/1997 14

TRUNC: returns a DATE truncated to the unit specified by the second argument. Date Function Examples: SELECT hire_date, TRUNC(hire_date, 'Month') FROM employees WHERE department_id=50; SELECT hire_date, TRUNC(hire_date, 'Year') FROM employees WHERE department_id=50; Result 16/Nov/1999 17/Oct/1995 29/Jan/1997 16/Nov/1999 17/Oct/1995 29/Jan/1997 01/Nov/1999 01/Oct/1995 01/Jan/1997 01/Jan/1999 01/Jan/1995 01/Jan/1997 15

Here is an example of a query using multiple date functions. The output is displayed on the next slide. SELECT employee_id, hire_date, ROUND(MONTHS_BETWEEN(SYSDATE, hire_date)) AS TENURE, ADD_MONTHS (hire_date, 6) AS REVIEW, NEXT_DAY(hire_date, 'FRIDAY'), LAST_DAY(hire_date) FROM employees WHERE MONTHS_BETWEEN (SYSDATE, hire_date) > 36; 16

The result set from this query returns 20 rows including: 17

Terminology Key terms used in this lesson included: ADD_MONTHS LAST_DAY MONTHS_BETWEEN NEXT_DAY SYSDATE ROUND TRUNC 18

Summary In this lesson, you should have learned how to: Select and apply the single-row functions MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, and TRUNC that operate on date data Explain how date functions transform Oracle dates into date data or numeric values Demonstrate proper use of the arithmetic operators with dates 19

Summary In this lesson, you should have learned how to: Demonstrate the use of SYSDATE and date functions State the implications for world businesses to be able to easily manipulate data stored in date format 20