Functions and string manipulation Lab 2 Week 2. Date functions Commonly used date functions are: –sysdate –next_day –add_months –last_day –months_between.

Slides:



Advertisements
Similar presentations
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL3.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Structured Query Language and its components. SQL SQL stands for Structured Query Language. There is a standard SQL called the American National Standards.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Some relationship types Using the Builder2 schema.
Databases Lab 5 Further Select Statements. Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time.
Starting out The RDBMS we will be using is Oracle. It is held on the FERDIA server. The Oracle client is installed in all of the labs in the COMP domain.
Sample queries Practice in using Oracle SQL (1 of 2)
1 Databases Semester 2 Week 1 Lab 2 The Delete, grant, revoke and Select Statements.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Databases Week 1, lab 2 Simple selects. About the environment We are using SQL Server for the moment. The server we are using is: –Cian.student.comp.dit.ie.
1 Databases. 2 Simple selects The full syntax of the SELECT statement is complex, but the main clauses can be summarized as: SELECT select_list [INTO.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Using Single-Row Functions to Customize Output
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Using Web Discoverer with AQS ADVANCED Way Poteat & Fletcher Clover USEPA – National Air Data Group 2012 AQS Conference – August 24, 2012 Providence, Rhode.
Single-Row Functions. SQL Functions Functions are a very powerful feature of SQL and can be used to do the following: Perform calculations on data Modify.
SQL functions - numeric and date Speaker notes contain additional information!
3-1 Copyright  Oracle Corporation, All rights reserved. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
3 Single-Row Functions. 3-2 Objectives At the end of this lesson, you should be able to: Describe various types of functions available in SQL Use character,
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
1 Data Manipulation in SQL  Department of Computer Science Northern Illinois University February 2001.
Oracle’s take on joins Where it differs from ANSI standard.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Single Row Functions Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number, date, general and conversion.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
SQL Oracle PL/SQL. Select SELECT column1, column2,...columnN FROM table_name WHERE condition; SELECT column1, column2,...columnN FROM table_name WHERE.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
6 Copyright © 2006, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Conversion Functions.
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
INTRODUCTION TO SQL Chapter SELECT * FROM teacher WHERE INSTR (subject_id, ‘&1’)= 4 AND LOWER (subject_id) LIKE ‘HST%’ ; When prompted for the.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
Copyright س Oracle Corporation, All rights reserved. 3 Single-Row Functions.
3 Copyright © Oracle Corporation, All rights reserved. Single-Row Functions.
3 第三讲 Single-Row Functions. Objectives After completing this lesson, you should be able to do the following: Describe various types of functions available.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
Built-in SQL Functions. 2 Type of Functions Character Functions returning character values returning numeric values Numeric Functions Date Functions Conversion.
3 Copyright © 2007, Oracle. All rights reserved. Substitution Variables ra Oly l&On nase lce Int erU.
4/2/16. Ltrim() is used to remove leading occurrences of characters. If we don’t specify a character, Oracle will remove leading spaces. For example Running.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
3 Copyright © 2004, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
Single Row Functions Part I Week 2. Objectives –Describe types of single row functions in SQL –Describe and use character, number and date SQL functions.
In this session, you will learn to:
Connect to SQL Server and run select statements
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
CS 3630 Database Design and Implementation
Using Single-Row Functions to Customize Output
Date Functions Farrokh Alemi, Ph.D.
Presentation transcript:

Functions and string manipulation Lab 2 Week 2

Date functions Commonly used date functions are: –sysdate –next_day –add_months –last_day –months_between –least –greatest –round –trunc

Date functions and the DUAL table. Current date and time: SQL> select sysdate from dual; SYSDATE SEP-05 Dual: – This supplies values for system variables. Current date and time is one of them. We will come across more later.

Sample date functions SQL> select 2 order_date as "Date", 3 to_char(order_date,'DAY'), 4 next_day(order_date,'MONDAY') as "Monday following", 5 last_day(order_date) as "Last day of month", 6 add_months (order_date,3) as "3 months later" 7 from corder;

Meaning… To_char(date,format) –Converts the date to the specified format. Next_day(date,dayofweek) –Gives the date of the next ‘dayofweek’ after the date given. Last_day(date) –Gives the last day of the month in the date specified. add_months (date,int) –Adds int months to the given date.

Produces… Date TO_CHAR(O Monday fo Last day 3 months FEB-02 SATURDAY 04-FEB FEB MAY FEB-05 FRIDAY 07-FEB FEB MAY FEB-05 SUNDAY 07-FEB FEB MAY FEB-05 THURSDAY 14-FEB FEB MAY FEB-05 SATURDAY 14-FEB FEB MAY FEB-05 FRIDAY 21-FEB FEB MAY FEB-05 TUESDAY 28-FEB FEB MAY FEB-05 SATURDAY 14-FEB FEB MAY rows selected.

Date functions Sysdate gives current date Next_day(d,day) where d is a date and day is a string representing a day of the week. –E.g. next_day(’14-dec-2005’,’Monday’) will return ’19-dec-2005’ Add_months(d,count) adds n months to d. Last_day(d) returns the date corresponding to the last day of the month in which d belongs. Months_between(d1,d2) Least(d1,d2,…,dn) Greatest(d1,…,dn) Trunc(d) returns the date (d) with the time at midnight.

Functions in SQL There are many types of functions provided. The ones that are used most are: –Date and Time functions –Mathematical functions –String functions There follows a list of all functions in these categories. We will practice only the most popularly used.

All about dates Dates are relative – i.e. the date and time are the same function. The current date and time depends on where you are in the world. The date format '12-dec-2005' will work, but NOT ’12-dec-2005’. –The apostrophes, or quotes, are different. Microsoft Word / PowerPoint will automatically change from ' to ‘.

Formatting the date To_char(d,format) returns the date in the format specified: –Mm returns month number –Mon returns the month in 3-character format –D returns the day number in the week –DD returns the day number in the month –DDD returns the day number in the year –DY gives the weekday in 3-character format –DAY gives the weekday name –Y returns the last digit of the year –Yy returns the last 2 digits of the year –Yyyy returns the 4-digit year –Hh12 returns the hours of the day(1 -12) –Hh24 returns the hours of the day (1 – 24) –Mi returns the minutes of the hour –Ss returns the seconds of the minute –AM returns AM or PM

resources These sites are helpful: – –

To put a name on a column Use the ‘as’ clause to give a name to a column. –Unitprice as Price or –UnitPrice as “Unit Price” –Note double quotes. This can be used on any column, but is especially useful in a derived column. New columns can be derived from existing fields: E.g. the value of an item in stock is the number in stock by the unit price. Surround the alias with double quotes: SQL> select stock_description as "Name" from stock; Name Brick - red, 30x100 Cavity blocks(100) 2"x4" lengths 6" Nails(50) 6" Nails(100) Workbench cordless Drill Cavity blocks(500) Cavity blocks(200) 9 rows selected. SQL>

Naming sample SQL> select 2 stock_description as "Name", 3 QuantityRequired as "Quantity", 4 Unit_Price as "at Price", 5 Unit_Price * QuantityRequired as "SubTotal" 6 from Stock join Corderline on 7 stock.stock_code = corderline.stock_code; Name Quantity at Price SubTotal Brick - red, 30x

String functions - Concatenation Concatenation: SQL> select supplier_name||','||supplier_address from supplier; SUPPLIER_NAME||','||SUPPLIER_ADDRESS Buckleys,Quarry town, Quarrysville, D44. Brendan Moore,44 Kevin St., D8 James McGovern,33 Synge St. Liam Keenan,33 Mount Vernon Ave Mary O'Brien,Appian Way, D2 Oliver Moore,Georges St., D2 June Browne,33 Liberty Lane Paul Sloan,44 Liberty Lane Kevin Kelly,33 Bride St, D8 Robert O'Mahony,Fitzwilliam Sq Patricia O'Brien,21 Liberty Lane, D8 11 rows selected.

String manipulation Concatenation: use || instead of, Pad out a string (from the left) to a specified length: –Lpad(string,length,’padding char’) –Rpad does the same, but pads from the right. Trim strings of characters uses –Ltrim(string,’trim char’) –Rtrim trims from the right.

Example of lpad SQL> select lpad(stock_code,12,'x') as "stock code", stock_description from stock; stock code STOCK_DESCRIPTION xxxxxxxxA111 Red bricks(100) xxxxxxxBRK11 Brick - red, 30x100 xxxxxxxxA101 Cavity blocks(100) xxxxxxxxB101 2"x4" lengths xxxxxxxxB111 Window Frames 2'x4' xxxxxxxxC101 6" Nails(50) xxxxxxxxC121 6" Nails(100) xxxxxxxxD101 Workbench xxxxxxxxD131 cordless Drill xxxxxxxxE101 Cavity blocks(500) xxxxxxxxE141 Cavity blocks(200) 11 rows selected.

Trim from the right SQL> select stock_code, rtrim(stock_code,'1') from stock; STOCK RTRIM A101 A10 A111 A B101 B10 B111 B BRK11 BRK C101 C10 C121 C12 D101 D10 D131 D13 E101 E10 E141 E14 11 rows selected.

Look up… Lower(string) Upper(string) Length(string) Substr(string,start,[n]) Instr(string,’chars’[,start [,n]])

Exercises Retrieve the system date and display it in 5 different formats. To demonstrate the effectiveness of ‘order by’: –Select all from the builder’s sorderline table –Select all from the builder’s sorderline table in order of the quantity of stock that is required. –Select all from the builder’s sorderline table in descending order of the quantity of stock that is required.

More exercises Show the order id, the customer id and the day, month and year of the order date, for all orders, in customer id order. Display the supplierOrderDate, the DeliveredDate and the difference in days between the two, for all orders that have been shipped (i.e. deliveredDate is not null)

More exercises Display the order id, the required date, the shipped date where the shipped date is after the required date, the number of days by which it is late, ordered by the number of days by which it is late. Display the orderdate and two weeks after the orderdate for each order that has not been shipped (shippeddate is null).

Exercises Amend the previous exercises to put names on the columns. Format your outputs so that they are easy and pleasant to read. Try out the ‘where’ clause on the sample database, changing the conditions.

Achievements Writing Basic SQL Select Statements –[√]List the capabilities of SQL SELECT statements –[ √ ]Execute a basic SELECT statement Restricting and Sorting Data –[ √] Limit the rows retrieved by a query –[ √] Sort the rows retrieved by a query Single-Row Functions –[ √] Describe various types of functions available in SQL –[ √] Use character, number, and date functions in SELECT statements –[ √] Use conversion functions