SQL Use of Functions Character functions Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Relational database - student system As always please use speaker notes!
Advertisements

Creating an Oracle Database Table Additional information is available in speaker notes!
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Copyright © 2007, Oracle. All rights reserved Using Single-Row Functions to Customize Output Modified: October 21, 2014.
Reports Using SQL Script Please check speaker notes for additional information!
Order Entry System Please use speaker notes for additional information!
Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the speaker notes!
Using input variables Please use speaker notes for additional information!
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Using Single-Row Functions to Customize Output
Lecture 6 29/1/15. Number functions Number functions take numbers as input, change them, and output the results as numbers. 2.
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.
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
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.
Manipulation Masterclass By the VB Gods. In this masterclass, we will learn how to use some of the string manipulation function such as Len, Right, Left,
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.
Introduction to Tables/Arrays Please use the speaker notes for additional information. Tables/Arrays.
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
More on relational databases, including 1 to 1, 1 to many and many to many relationships Please use speaker notes for additional information!
XML, DTD, ENTITY Please see speaker notes for additional information!
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
XML and DTD Please you speaker notes for additional information!
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 from the Trenches Presented by: Robert Arce.
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
Manipulating data within PL/SQL Please use speaker notes for additional information!
Functions Please use speaker notes for additional information!
Exceptions in PL/SQL Please use speaker notes for additional information!
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Single Row Functions. Objectives –Use character, number, and date functions –Use conversion functions –Describe types of single row functions in SQL.
Introduction to Oracle - SQL Additional information is available in speaker notes!
Label Assignment Please use speaker notes for additional information!
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
SQL Functions. SQL functions are built into Oracle Database and are available for use in various appropriate SQL statements. These functions are use full.
Programming Assignment #2 in CIS12 Please use speaker notes for additional information!
1 Session 6: Database Best Practice iNET Academy Open Source Web Development.
More on Primary and Foreign Keys Please see speaker notes for additional information!
Introduction to PL/SQL As usual, use speaker notes for additional information!
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.
PHP with MYSQL Please use speaker notes for additional information!
More about maintaining a table Use speaker notes for additional information!
More SQL functions As usual,there is additional information in the speaker notes!
3 Copyright © 2009, Oracle. All rights reserved. Using Single-Row Functions to Customize Output.
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.
Open Source Server Side Scripting MySQL Functions
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
Introduction to Procedures
MySQL - Creating donorof database offline
Using Single-Row Functions to Customize Output
SQL Text Manipulation Farrokh Alemi, Ph.D.
Introduction to Views and Reports
CS122 Using Relational Databases and SQL
Creating Tables & Inserting Values Using SQL
More and Still More on Procedures and Functions
String Processing 1 MIS 3406 Department of MIS Fox School of Business
MapInfo SQL String Functions
Presentation transcript:

SQL Use of Functions Character functions Please use speaker notes for additional information!

Character functions PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG rows selected. SQL> SELECT name, INITCAP(jobcode) 2 FROM first_pay; NAME IN Linda Costa Ci John Davidson In Susan Ash Ap Stephen York Cm Richard Jones Ci Joanne Brown In 6 rows selected. INITCAP is a function that shows the data converted to an initial capital followed by lower case. In this example, jobcode was originally in uppercase. Now there is an initial capital followed by a lower case letter. Note: The column header now says IN. This would be an appropriate time for an alias.

Character functions SQL> SELECT INITCAP(startdate) 2 FROM first_pay; INITCAP(STARTDATE) Jan Sep Feb Jul Oct-92 On the table, the months are stored in uppercase, here they are shown with an initial capital followed by lower case. In this example, you can clearly see the function included in the column header. I am using the function to show particular words with initial capitals. SQL> SELECT INITCAP('mrs. grocer') 2 FROM dual; INITCAP('MR Mrs. Grocer

Character functions SQL> SELECT UPPER(name) 2 FROM first_pay; UPPER(NAME) LINDA COSTA JOHN DAVIDSON SUSAN ASH STEPHEN YORK RICHARD JONES JOANNE BROWN Originally the data was in mixed case, the UPPER function converts it to UPPER case for this display. SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Listing of the table after the UPPER function shows the data unaffected.

Character functions SQL> SELECT LOWER(name), LOWER(jobcode) 2 FROM first_pay; LOWER(NAME) LO linda costa ci john davidson in susan ash ap stephen york cm richard jones ci joanne brown in The LOWER function converts fields to lower case for display. NAME was originally in mixed case and jobcode was originally in upper case.

Character functions SQL> SELECT * 2 FROM first_pay 3 WHERE UPPER(jobcode) = 'CI'; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN Richard Jones CI 30-OCT Let’s say you have a big table and you don’t know whether some records have jobcode in upper case, some in lower case and some in mixed case. You want to find all CI jobcodes. The safest way to do this is to convert all of the jobcodes on the file to upper case for comparison purposes and then compare them to uppercase CI. This will guarantee you the best chance of finding all of the CI, ci, CI, cI job codes.

Character functions SQL> DESC char_table; Name Null? Type IDNO CHAR(4) NAME CHAR(20) CITY CHAR(15) STATE CHAR(2) SQL> SELECT * 2 FROM char_table; IDNO NAME CITY ST John Doe Seekonk MA 222 Susan Ash Taunton MA 3333 Linda Forest Providence RI For this example, I created a table made up of all character columns/fields. SQL> SELECT name || city || state 2 FROM char_table; NAME||CITY||STATE John Doe Seekonk MA Susan Ash Taunton MA Linda Forest Providence RI I then used the concatenate symbol - || - to show the name concatenated with the city concatenated with the state as one string. Notice the blanks because CHAR fields pad with spaces to make the full length. If this had been done with VARCHAR2 fields, you would see the fields together without the space padding. Remember, each of these rows is one string field.

SQL> SELECT RTRIM(name) || RTRIM(city) || state 2 FROM char_table; RTRIM(NAME)||RTRIM(CITY)||STATE John DoeSeekonkMA Susan AshTauntonMA Linda ForestProvidenceRI Character functions By doing a RTRIM on name and city, I have eliminated the extra spaces and I see only the data. This is now strong together as one field containing just the data. A more readable example with comma and space concatenated in to the string is shown below. SQL> SELECT RTRIM(name) || ', ' || RTRIM(city) || ', ' || state 2 FROM char_table; RTRIM(NAME)||','||RTRIM(CITY)||','||STATE John Doe, Seekonk, MA Susan Ash, Taunton, MA Linda Forest, Providence, RI

Character functions SQL> SELECT ' CIS50', LTRIM(' CIS50') 2 FROM DUAL; 'CIS50' LTRIM CIS50 CIS50 The LTRIM function trims spaces from the left.

Character functions SQL> SELECT RPAD(name,20,'-'), LPAD(salary,9,'*'), LPAD(bonus,5,'$') 2 FROM first_pay; RPAD(NAME,20,'-') LPAD(SALA LPAD( Linda Costa ****45000 $1000 John Davidson ****40000 $1500 Susan Ash ****25000 $$500 Stephen York ****42000 $2000 Richard Jones ****50000 $2000 Joanne Brown ****48000 $2000 The RPAD function pads to the right and the LPAD function pads to the left. In this example, name is right padded to its length of 20 characters with the -. Salary is left padded with * to its length of 9 and bonus is left padded with $ to its length of 5. This kind of padding can be especially important with numeric fields that you do not want altered.

Character functions SQL> SELECT SUBSTR(datefst,4,3), datefst 2 FROM donor; SUB DATEFST JUL 03-JUL-98 MAY 24-MAY-97 JAN 03-JAN-98 MAR 04-MAR-92 APR 04-APR-98 SUBSTR can be used to extract certain characters of data from a data string. In this case, I am extracting the month. The month starts in position 4 and goes for 3 characters. Therefore I use SUBSTR(datefst,4,3). name of column/field position of first character of substring length of substring SQL> SELECT SUBSTR(datefst,4) 2 FROM donor; SUBSTR(DATEFST,4) JUL-98 MAY-97 JAN-98 MAR-92 APR-98 NOTE: If length is not specified, you will get everything from the start point on.

Character functions SQL> SELECT * FROM donor; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT Stephen Daniels 123 Elm St Seekonk MA JUL John Smith Jennifer Ames 24 Benefit St Providence RI MAY Susan Jones Carl Hersey 24 Benefit St Providence RI JAN-98 Susan Jones Susan Ash 21 Main St Fall River MA MAR Amy Costa Nancy Taylor 26 Oak St Fall River MA MAR John Adams Robert Brooks 36 Pine St Fall River MA APR Amy Costa 6 rows selected. SQL> SELECT datefst, INSTR(datefst,'A') 2 FROM donor; DATEFST INSTR(DATEFST,'A') JUL MAY JAN MAR APR rows selected. No A in JUL A in 5th character position in MAY A in 5th character position in JAN A in 5th character position in MAR A in 5th character position in MAR A in 4th character position in APR column/field being examined character being looked for

Character functions SQL> SELECT name, LENGTH(name), stadr, LENGTH(stadr), city, LENGTH(city) 2 FROM donor; NAME LENGTH(NAME) STADR LENGTH(STADR) CITY LENGTH(CITY) Stephen Daniels Elm St 10 Seekonk 7 Jennifer Ames Benefit St 13 Providence 10 Carl Hersey Benefit St 13 Providence 10 Susan Ash 9 21 Main St 10 Fall River 10 Nancy Taylor Oak St 9 Fall River 10 Robert Brooks Pine St 10 Fall River 10 LENGTH tells the length of the characters entered into the column/field. NOTE: Embedded spaces are counted.

Character functions SQL> desc char_table; Name Null? Type IDNO CHAR(4) NAME CHAR(20) CITY CHAR(15) STATE CHAR(2) SQL> SELECT idno, LENGTH(idno), name, LENGTH(name), city, LENGTH(city), state, LENGTH(state) 2 FROM char_table; IDNO LENGTH(IDNO) NAME LENGTH(NAME) CITY LENGTH(CITY) ST LENGTH(STATE) John Doe 20 Seekonk 15 MA Susan Ash 20 Taunton 15 MA Linda Forest 20 Providence 15 RI 2 This table has all CHAR data. This means the data is padded with spaces. Notice that the length field is the length given when the table was created and not the length of the data stored in the field.

Character functions SQL> desc first_pay; Name Null? Type PAY_ID VARCHAR2(4) NAME VARCHAR2(20) JOBCODE CHAR(2) STARTDATE DATE SALARY NUMBER(9,2) BONUS NUMBER(5) SQL> SELECT salary, LENGTH(salary), bonus, LENGTH(bonus) 2 FROM first_pay; SALARY LENGTH(SALARY) BONUS LENGTH(BONUS)

Character functions SQL> SELECT jobcode, REPLACE(jobcode,'CI','IT') 2 FROM first_pay; JO REPL CI IT IN AP CM CI IT IN In this example, all rows/records that contain CI as the jobcode are displayed with IT as the jobcode.

Character functions SQL> SELECT SUBSTR(startdate,4,3) || ' ' || SUBSTR(startdate,1,2) || ', ' || SUBSTR(startdate,8,2) 2 FROM first_pay; SUBSTR(STA JAN 15, 97 SEP 25, 92 FEB 05, 00 JUL 03, 97 OCT 30, 92 AUG 18, 94 This code extracts the month for the date, concatenates it with a space, then extracts the day from the date, concatenates it with a comma space and extracts the year from the date.

SQL> SELECT SUBSTR(UPPER(name),1,2) || SUBSTR(stadr,1,INSTR(stadr,' ')-1) || SUBSTR(idno,4,2) 2 FROM donor; SUBSTR(UPPER(NAME), ST12311 JE2421 CA2422 SU2156 NA2633 RO rows selected. Character function SUBSTR(UPPER(name),1,2) First UPPER converts the name to upper case. Then SUBSTR takes the upper case name starts at character 1 and extracts 2 characters. The characters are therefore the first two characters of the name. SUBSTR(stadr,1,INSTR(stadr,' ')-1) This code will extract a substring from stadr. It will start with the first character. The number of characters taken will be determined by using INSTR to find the space in the street address and then subtract 1 from it. Essentially this gives you the street number. Note that INSTR is determine before SUBSTR. SUBSTR(idno,4,2) This code will start with the fourth character of the column/field idno and extract two characters. In other words, it will extract the fourth and fifth characters.