Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Databases

2 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 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 4 Date functions Commonly used date functions are: –sysdate –next_day –add_months –last_day –months_between –least –greatest –round –trunc

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

6 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 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 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 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 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 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 12 resources These sites are helpful: –http://www.techonthenet.com/oracle/index.phphttp://www.techonthenet.com/oracle/index.php –http://www.ss64.com/orasyntax/http://www.ss64.com/orasyntax/

13 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 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 15 Look up… Lower(string) Upper(string) Length(string) Substr(string,start,[n]) Instr(string,’chars’[,start [,n]])

16 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 69.6937855 The Legend of Captain Crow's teeth 33.6937855 Don't Open that Box 93.6937855 Benny and Babe 129.693785 Artemis Fowl 81.6937855 Harry Potter and the Goblet of Fire 98.5324951 Harry Potter and the Philosopher's Stone 86.5324951 Harry Potter and the Chamber of Secrets 74.5324951 Harry Potterand the Prisoner of Azkhaban 62.5324951 Harry Potter + the Order of the Phoenix 38.5324951 Harry Potter and the Half-Blood Prince 14.5324951 Cirque Du Freak 44.5324951 Tunnel of Blood 23.5324951 Up, up and Away 189.693785 Tom and Pippo 249.693785

17 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 250 15 rows selected.

18 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 21 15 rows selected.

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


Download ppt "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."

Similar presentations


Ads by Google