SlideShare a Scribd company logo
1 of 18
SQL Use of Functions
Character functions
Please use speaker notes for
additional information!
Character functionsCharacter functions
PAY_ NAME JO STARTDATE SALARY BONUS
---- -------------------- -- --------- --------- ---------
1111 Linda Costa CI 15-JAN-97 45000 1000
2222 John Davidson IN 25-SEP-92 40000 1500
3333 Susan Ash AP 05-FEB-00 25000 500
4444 Stephen York CM 03-JUL-97 42000 2000
5555 Richard Jones CI 30-OCT-92 50000 2000
6666 Joanne Brown IN 18-AUG-94 48000 2000
6 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 functionsCharacter functions
SQL> SELECT INITCAP(startdate)
2 FROM first_pay;
INITCAP(STARTDATE)
----------------------------------
15-Jan-97
25-Sep-92
05-Feb-00
03-Jul-97
30-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 functionsCharacter 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
---- -------------------- -- --------- --------- ---------
1111 Linda Costa CI 15-JAN-97 45000 1000
2222 John Davidson IN 25-SEP-92 40000 1500
3333 Susan Ash AP 05-FEB-00 25000 500
4444 Stephen York CM 03-JUL-97 42000 2000
5555 Richard Jones CI 30-OCT-92 50000 2000
6666 Joanne Brown IN 18-AUG-94 48000 2000
Listing of the table
after the UPPER
function shows the
data unaffected.
Character functionsCharacter 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 functionsCharacter functions
SQL> SELECT *
2 FROM first_pay
3 WHERE UPPER(jobcode) = 'CI';
PAY_ NAME JO STARTDATE SALARY BONUS
---- -------------------- -- --------- --------- ---------
1111 Linda Costa CI 15-JAN-97 45000 1000
5555 Richard Jones CI 30-OCT-92 50000 2000
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 functionsCharacter 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
---- -------------------- --------------- --
11 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 functionsCharacter 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 functionsCharacter functions
SQL> SELECT ' CIS50', LTRIM(' CIS50')
2 FROM DUAL;
'CIS50' LTRIM
----------- -----
CIS50 CIS50 The LTRIM function trims spaces from
the left.
Character functionsCharacter 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 functionsCharacter 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
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
MAR-92
APR-98
NOTE: If length is not
specified, you will get
everything from the
start point on.
Character functionsCharacter functions
SQL> SELECT * FROM donor;
IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT
----- --------------- --------------- ---------- -- ----- --------- --------- ------------
11111 Stephen Daniels 123 Elm St Seekonk MA 02345 03-JUL-98 500 John Smith
12121 Jennifer Ames 24 Benefit St Providence RI 02045 24-MAY-97 400 Susan Jones
22222 Carl Hersey 24 Benefit St Providence RI 02045 03-JAN-98 Susan Jones
23456 Susan Ash 21 Main St Fall River MA 02720 04-MAR-92 100 Amy Costa
33333 Nancy Taylor 26 Oak St Fall River MA 02720 04-MAR-92 50 John Adams
34567 Robert Brooks 36 Pine St Fall River MA 02720 04-APR-98 50 Amy Costa
6 rows selected.
SQL> SELECT datefst, INSTR(datefst,'A')
2 FROM donor;
DATEFST INSTR(DATEFST,'A')
--------- ------------------
03-JUL-98 0
24-MAY-97 5
03-JAN-98 5
04-MAR-92 5
04-MAR-92 5
04-APR-98 4
6 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 functionsCharacter 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 15 123 Elm St 10 Seekonk 7
Jennifer Ames 13 24 Benefit St 13 Providence 10
Carl Hersey 11 24 Benefit St 13 Providence 10
Susan Ash 9 21 Main St 10 Fall River 10
Nancy Taylor 12 26 Oak St 9 Fall River 10
Robert Brooks 13 36 Pine St 10 Fall River 10
LENGTH tells the length of the characters entered into the column/field.
NOTE: Embedded spaces are counted.
Character functionsCharacter 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)
---- ------------ -------------------- ------------ --------------- ------------ -- -------------
11 4 John Doe 20 Seekonk 15 MA 2
222 4 Susan Ash 20 Taunton 15 MA 2
3333 4 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 functionsCharacter 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)
--------- -------------- --------- -------------
45000 5 1000 4
40000 5 1500 4
25000 5 500 3
42000 5 2000 4
50000 5 2000 4
48000 5 2000 4
Character functionsCharacter functions
SQL> SELECT jobcode, REPLACE(jobcode,'CI','IT')
2 FROM first_pay;
JO REPL
-- ----
CI IT
IN IN
AP AP
CM CM
CI IT
IN IN
In this example, all rows/records that
contain CI as the jobcode are displayed
with IT as the jobcode.
Character functionsCharacter 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
RO3667
6 rows selected.
Character functionCharacter 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.

More Related Content

What's hot (17)

SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3SQL WORKSHOP::Lecture 3
SQL WORKSHOP::Lecture 3
 
Sql
SqlSql
Sql
 
Cassandra model
Cassandra modelCassandra model
Cassandra model
 
Procedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom TableProcedure To Store Database Object Size And Number Of Rows In Custom Table
Procedure To Store Database Object Size And Number Of Rows In Custom Table
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
11 things about 11gr2
11 things about 11gr211 things about 11gr2
11 things about 11gr2
 
Les10
Les10Les10
Les10
 
COIS 420 - Practice01
COIS 420 - Practice01COIS 420 - Practice01
COIS 420 - Practice01
 
Sql statements function join
Sql statements function joinSql statements function join
Sql statements function join
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
Les01-Oracle
Les01-OracleLes01-Oracle
Les01-Oracle
 
Oracle naveen Sql
Oracle naveen   SqlOracle naveen   Sql
Oracle naveen Sql
 
Lab
LabLab
Lab
 
Les04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple TableLes04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple Table
 
intern_showcase_FINAL
intern_showcase_FINALintern_showcase_FINAL
intern_showcase_FINAL
 
Intro To TSQL - Unit 2
Intro To TSQL - Unit 2Intro To TSQL - Unit 2
Intro To TSQL - Unit 2
 

Similar to SQL OUR NEEDS

Database Management System
Database Management SystemDatabase Management System
Database Management SystemHitesh Mohapatra
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلMohamed Moustafa
 
data constraints,group by
data constraints,group by data constraints,group by
data constraints,group by Visakh V
 
CompanyDB Problemspage 1 of 3Consider the employee database of .docx
CompanyDB Problemspage 1 of 3Consider the employee database of .docxCompanyDB Problemspage 1 of 3Consider the employee database of .docx
CompanyDB Problemspage 1 of 3Consider the employee database of .docxmonicafrancis71118
 
Database management system file
Database management system fileDatabase management system file
Database management system fileAnkit Dixit
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with outputNexus
 
Mysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERTMysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERTHarish Gyanani
 
Oracle12c For Developers
Oracle12c For DevelopersOracle12c For Developers
Oracle12c For DevelopersAlex Nuijten
 
SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所Hiroshi Sekiguchi
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query TuningAlexander Rubin
 

Similar to SQL OUR NEEDS (20)

Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
Les01
Les01Les01
Les01
 
Les01
Les01Les01
Les01
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
 
data constraints,group by
data constraints,group by data constraints,group by
data constraints,group by
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
 
CompanyDB Problemspage 1 of 3Consider the employee database of .docx
CompanyDB Problemspage 1 of 3Consider the employee database of .docxCompanyDB Problemspage 1 of 3Consider the employee database of .docx
CompanyDB Problemspage 1 of 3Consider the employee database of .docx
 
Database management system file
Database management system fileDatabase management system file
Database management system file
 
Basic sql statements
Basic sql statementsBasic sql statements
Basic sql statements
 
Sql 3
Sql 3Sql 3
Sql 3
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
Mysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERTMysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERT
 
Function and types
Function  and typesFunction  and types
Function and types
 
Oracle12c For Developers
Oracle12c For DevelopersOracle12c For Developers
Oracle12c For Developers
 
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Oracle12 for Developers - Oracle OpenWorld Preview AMISOracle12 for Developers - Oracle OpenWorld Preview AMIS
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
 
SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所SQLチューニング総合診療Oracle CloudWorld出張所
SQLチューニング総合診療Oracle CloudWorld出張所
 
Advanced MySQL Query Tuning
Advanced MySQL Query TuningAdvanced MySQL Query Tuning
Advanced MySQL Query Tuning
 

Recently uploaded

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

SQL OUR NEEDS

  • 1. SQL Use of Functions Character functions Please use speaker notes for additional information!
  • 2. Character functionsCharacter functions PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 6 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.
  • 3. Character functionsCharacter functions SQL> SELECT INITCAP(startdate) 2 FROM first_pay; INITCAP(STARTDATE) ---------------------------------- 15-Jan-97 25-Sep-92 05-Feb-00 03-Jul-97 30-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
  • 4. Character functionsCharacter 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 Listing of the table after the UPPER function shows the data unaffected.
  • 5. Character functionsCharacter 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.
  • 6. Character functionsCharacter functions SQL> SELECT * 2 FROM first_pay 3 WHERE UPPER(jobcode) = 'CI'; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 5555 Richard Jones CI 30-OCT-92 50000 2000 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.
  • 7. Character functionsCharacter 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 ---- -------------------- --------------- -- 11 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.
  • 8. SQL> SELECT RTRIM(name) || RTRIM(city) || state 2 FROM char_table; RTRIM(NAME)||RTRIM(CITY)||STATE ------------------------------------- John DoeSeekonkMA Susan AshTauntonMA Linda ForestProvidenceRI Character functionsCharacter 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
  • 9. Character functionsCharacter functions SQL> SELECT ' CIS50', LTRIM(' CIS50') 2 FROM DUAL; 'CIS50' LTRIM ----------- ----- CIS50 CIS50 The LTRIM function trims spaces from the left.
  • 10. Character functionsCharacter 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.
  • 11. Character functionsCharacter 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 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 MAR-92 APR-98 NOTE: If length is not specified, you will get everything from the start point on.
  • 12. Character functionsCharacter functions SQL> SELECT * FROM donor; IDNO NAME STADR CITY ST ZIP DATEFST YRGOAL CONTACT ----- --------------- --------------- ---------- -- ----- --------- --------- ------------ 11111 Stephen Daniels 123 Elm St Seekonk MA 02345 03-JUL-98 500 John Smith 12121 Jennifer Ames 24 Benefit St Providence RI 02045 24-MAY-97 400 Susan Jones 22222 Carl Hersey 24 Benefit St Providence RI 02045 03-JAN-98 Susan Jones 23456 Susan Ash 21 Main St Fall River MA 02720 04-MAR-92 100 Amy Costa 33333 Nancy Taylor 26 Oak St Fall River MA 02720 04-MAR-92 50 John Adams 34567 Robert Brooks 36 Pine St Fall River MA 02720 04-APR-98 50 Amy Costa 6 rows selected. SQL> SELECT datefst, INSTR(datefst,'A') 2 FROM donor; DATEFST INSTR(DATEFST,'A') --------- ------------------ 03-JUL-98 0 24-MAY-97 5 03-JAN-98 5 04-MAR-92 5 04-MAR-92 5 04-APR-98 4 6 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
  • 13. Character functionsCharacter 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 15 123 Elm St 10 Seekonk 7 Jennifer Ames 13 24 Benefit St 13 Providence 10 Carl Hersey 11 24 Benefit St 13 Providence 10 Susan Ash 9 21 Main St 10 Fall River 10 Nancy Taylor 12 26 Oak St 9 Fall River 10 Robert Brooks 13 36 Pine St 10 Fall River 10 LENGTH tells the length of the characters entered into the column/field. NOTE: Embedded spaces are counted.
  • 14. Character functionsCharacter 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) ---- ------------ -------------------- ------------ --------------- ------------ -- ------------- 11 4 John Doe 20 Seekonk 15 MA 2 222 4 Susan Ash 20 Taunton 15 MA 2 3333 4 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.
  • 15. Character functionsCharacter 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) --------- -------------- --------- ------------- 45000 5 1000 4 40000 5 1500 4 25000 5 500 3 42000 5 2000 4 50000 5 2000 4 48000 5 2000 4
  • 16. Character functionsCharacter functions SQL> SELECT jobcode, REPLACE(jobcode,'CI','IT') 2 FROM first_pay; JO REPL -- ---- CI IT IN IN AP AP CM CM CI IT IN IN In this example, all rows/records that contain CI as the jobcode are displayed with IT as the jobcode.
  • 17. Character functionsCharacter 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.
  • 18. 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 RO3667 6 rows selected. Character functionCharacter 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.