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.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

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.
Functions and string manipulation Lab 2 Week 2. Date functions Commonly used date functions are: –sysdate –next_day –add_months –last_day –months_between.
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.
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.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Using Single-Row Functions to Customize Output
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,
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Oracle FUNCTIONS. Comment ScreenShot (in 10g) General Example of null Foreign Key: create table deptcs( deptno NUMBER(4) primary key, hiredate DATE,
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
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.
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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
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.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
Copyright  Oracle Corporation, All rights reserved. 3 Single-Row Functions.
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 第三讲 Single-Row Functions. Objectives After completing this lesson, you should be able to do the following: Describe various types of functions available.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Chuck’s Academy of Function Anatomy An ACEware Webinar Part 2.
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.
Lecture3b - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data Guide to Oracle 10g ITBIS373 Database Development.
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.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
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
SQL – Dates and Times.
Using Single-Row Functions to Customize Output
Date Functions Farrokh Alemi, Ph.D.
J.K Rowling A brief presentation concerning Rowling as an author and the creator of a loved book series.
Section 4 - Sorting/Functions
Lecture 5 SQL FUNCTIONS.
Introduction to SQL Server and the Structure Query Language
Temporal Data Part V.
Presentation transcript:

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 new_table_name] FROM table_list [WHERE search_conditions] [GROUP BY group_by_list] [HAVING search_conditions] [ORDER BY order_list [ASC | DESC] ] In this lesson, we will address only those clauses in black.

3 Projection Projection picks only rows that satisfy a condition : Pick only books with Harry Potter in the title. Note that each row wraps around, making it difficult to read. Find the tables SQL> select * from cat where table_name not like 'BIN$%‘;

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

5 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.

6 Sample date functions SQL> SELECT 2 datepublished AS "Date", 3 TO_CHAR(datepublished,'DAY'), 4 NEXT_DAY(datepublished,'MONDAY') AS "Monday following", 5 LAST_DAY(datepublished) AS "Last day of month", 6 ADD_MONTHS (datepublished,3) AS "3 months later" 7 FROM BOOK;

7 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.

8 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.

9 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.

10 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 ‘.

11 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

12 resources These sites are helpful: – –

13 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. If the alias contains a space, surround it with double quotes: SQL> SELECT datepublished AS “Date Published" FROM book;

14 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.

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

16 Exercises Write a query to return today’s date. Write a query to return the title of each book and the number of months since it was published, giving the following output: The Legend of Spud Murphy The Legend of Captain Crow's teeth Don't Open that Box Benny and Babe Artemis Fowl Harry Potter and the Goblet of Fire Harry Potter and the Philosopher's Stone Harry Potter and the Chamber of Secrets Harry Potterand the Prisoner of Azkhaban Harry Potter + the Order of the Phoenix Harry Potter and the Half-Blood Prince Cirque Du Freak Tunnel of Blood Up, up and Away Tom and Pippo

17 Refine: Refine your query, to round the months: Harry Potter and the Half-Blood Prince 15 Cirque Du Freak 45 Tunnel of Blood 24 Up, up and Away 190 Tom and Pippo rows selected.

18 Refine it again, to return years, instead of months Harry Potter and the Half-Blood Prince 1 Cirque Du Freak 4 Tunnel of Blood 2 Up, up and Away 16 Tom and Pippo rows selected.

19 Refine the output Concatenate (string together) the results (replace the, with ||): The Legend of Spud Murphy6 The Legend of Captain Crow's teeth3 Don't Open that Box8 Benny and Babe11 Artemis Fowl7 Harry Potter and the Goblet of Fire8 Harry Potter and the Philosopher's Stone7 Harry Potter and the Chamber of Secrets6 Harry Potterand the Prisoner of Azkhaban5 Harry Potter + the Order of the Phoenix3 Harry Potter and the Half-Blood Prince1 Cirque Du Freak4 Tunnel of Blood2 Up, up and Away16 Tom and Pippo21 15 rows selected.

20 Embed text strings in the output E.g. Select ‘Title ‘||title… Title The Legend of Spud Murphy6 Title The Legend of Captain Crow's teeth3 Title Don't Open that Box8 Title Benny and Babe11 Title Artemis Fowl7 Title Harry Potter and the Goblet of Fire8 Title Harry Potter and the Philosopher's Stone7 Title Harry Potter and the Chamber of Secrets6 Title Harry Potterand the Prisoner of Azkhaban5 Title Harry Potter + the Order of the Phoenix3 Title Harry Potter and the Half-Blood Prince1 Title Cirque Du Freak4 Title Tunnel of Blood2 Title Up, up and Away16 Title Tom and Pippo21 15 rows selected.

21 Now add more text… The title 'The Legend of Spud Murphy' is 6 years old The title 'The Legend of Captain Crow's teeth' is 3 years old The title 'Don't Open that Box' is 8 years old The title 'Benny and Babe' is 11 years old The title 'Artemis Fowl' is 7 years old The title 'Harry Potter and the Goblet of Fire' is 8 years old The title 'Harry Potter and the Philosopher's Stone' is 7 years old The title 'Harry Potter and the Chamber of Secrets' is 6 years old The title 'Harry Potterand the Prisoner of Azkhaban' is 5 years old The title 'Harry Potter + the Order of the Phoenix' is 3 years old The title 'Harry Potter and the Half-Blood Prince' is 1 years old The title 'Cirque Du Freak' is 4 years old The title 'Tunnel of Blood' is 2 years old The title 'Up, up and Away' is 16 years old The title 'Tom and Pippo' is 21 years old 15 rows selected. SQL>

22 And add a heading name Titles and their ages The title 'The Legend of Spud Murphy' is 6 years old The title 'The Legend of Captain Crow's teeth' is 3 years old The title 'Don't Open that Box' is 8 years old The title 'Benny and Babe' is 11 years old The title 'Artemis Fowl' is 7 years old The title 'Harry Potter and the Goblet of Fire' is 8 years old The title 'Harry Potter and the Philosopher's Stone' is 7 years old The title 'Harry Potter and the Chamber of Secrets' is 6 years old The title 'Harry Potterand the Prisoner of Azkhaban' is 5 years old The title 'Harry Potter + the Order of the Phoenix' is 3 years old The title 'Harry Potter and the Half-Blood Prince' is 1 years old The title 'Cirque Du Freak' is 4 years old The title 'Tunnel of Blood' is 2 years old The title 'Up, up and Away' is 16 years old The title 'Tom and Pippo' is 21 years old 15 rows selected. Remember, to put in a heading with spaces, the heading needs to be surrounded by double quotes.