Operations and Functions in SQL
Operations and Functions in SQL
Example
1. Select product_no, description and compute sell price 0.05 and sell_price 1.05
for each row retrieved;
SELECT product_no, description, sell price
FROM product_master,
0.05, sell price 1.05
Here, sell_price *
0.05 and sell price 1.05 are not columns in the table
but are calculations done on the contents of the product_master,
column sell price of the table
product master.
By default, the DBA will use the column of the table
output to the user on screen.
names
product master to display
Since there are no columns with names as
table product_master, the DBA will sell_price 0.05 and sell _price 1.05 in the
as the default column
perform the required operations and use these names
names when
displaying the output as shown below:
Product No Description Sell Price 0.05 Sell Price* 1.05
PO0001 1.44 Floppies 25
P03453 525
Monitors 600 12600
P06734 Mouse 50 1050
PO7865 1.22 Floppies 25 525
PO7868 Keyboards 150
PO7885 3150
CD Drive 250
PO7965 HDD
5250
400 8400
PO7975 1.44 Drive
PO8865 50 1050
1.22 Drive 50 1050
DEVELOPER
2000
ORACLE 7,
USING
DEVELOPMENT
APPLICATION
C O M M E R C I A L Lists:
Expression if required.
Used With by the
user
Renaming Columns
renamed
names can be
column
The default output columnname
r e s u l t _ c o l u m n n aame
me
result_columnname,
columnname
SELECT
Syntax
Syntax
FROMtablename;
Increase
New Price
Product No Description
25 525
PO0001 1.44 Floppies
600 12600
P03453 Monitors
50 1050
P06734 Mouse
P07865 1.22 Floppies 25 525
150 3150
PO7868 Keyboards
PO7885 CD Drive 250 5250
PO7965 HDD 400 8400
P07975 1.44 Drive 50 1050
PO8865 1.22 Drive 50 1050
LOGICAL OPERATORS
The logical operators that can be used in SQL sentences are
and all of must be included
or any of may be included
not none of would be included
Examples
1. Select client information like client ode
for all the clients in 'BOMBAY' or
no, name, address1, address2, city and plnco
'DELHT;
SELECT client_no, name, address1, address2, city,
FROM client_master WHERE pincode
city "BOMBAY' OR city "DELHI,
=
=
INTERACTIVE SQL 35
2. Select client information like client no, name, address1, address2, city and pincode
for all the clients in Santacruz(w) or Vile
Parle (E);
SELECT client no, name, address1, address2, city, pincode
FROM client_master WHERE city =
"BOMBAY' AND
(pincode = 400054 OR pincode = 400057);
Select produet no, description, profit_percent, sell_price where profit percent is not
2.
between 10 and 20;
Examples
I. Select supplier_name from supplier_master where the first two characters of name
ja'
SELECT supplier name FROM supplier_master
WHERE supplier name LIKE ja%;
6 COMMRCIAL APPLICATON DEVELOPMENT USING ORACLE 7, DEVELOPER 2000
2 Select supplier name from supplier _master where the second character of
or h name is 'r
na.
Examples
AVG Syntax AVG([DISTINCT|ALL] n)
Purpose Returns average value of n, ignoring null values
Example SELECT AVG(sell_price) "Average"
FROM product_master,
Output Average
2012.3654
Note In the above SELECT statement, AVG function is used to calculate the average selling price of
all products. The selected column is renamed as 'Average' in the
output.
MIN Syntax MIN([DISTINCT ALL expr)
Purpose Returns minimum value of expr.
Example SELECT MIN(s _order _date) "Minimum Date"
FROMsales_order
Output Minimum Date
26-Jan-93
15
5000.00
DEVELOPER
2000
ORACLE 7,
APPLICATION DEVEL OPMENT USING
vMERCIAL
SUM([DISTINCTALL] n)
SUM Syntax of values of
n.
Purpose
Returns sum
ordered)
"Total Qty"
SELECT SUM(qty
Example FROM sales_order_details
product_no
=
"P00001
WHERE
Total Qty
Output
29025.00
15
15.2
LOWER Syntax
Purpose LOWER(char)
Returns
Example char, with all letters in
SELECT lowercase
LOWER(TVAN
FROM dual BAYROSS) "Lower"
Output Lowen
ivan bayross
INTERACTIVE SQL 39
INITCAP Syntax INITCAP(char)
Purpose Returns string with the first letter in upper case
Example SELECT INITCAP(IVAN BAYROSS') "Title Case"
FROM dual;
Output Title Case
Ivan Bayross
XxxLAST wORD
TURNERxxX
0 COMMERCIAL APPLICATION DEVELOPMENT USING ORACLE 7, DEVELOPER 2000
*** Page 1
TURNERxxxx
TO_NUMBER Syntax TONUMBER(char)
Purpose Converts char, a character value containing a number, to
a value of NUMBER
datatype.
Example UPDATE product_master SET sell_price =
sell_price+
TO_NUMBER(SUBSTR($100',2,3);
TO DATE Syntax TO DATE(char [, fmt))
Purpose Converts a character field to a date field.
Example INSERT INTO sales order
(s_order no,
s_order_date)
VALUES (087650°, TO DATE( 30-SEP-85 10:55 A.M.',
DD-MON-YY HH:MI A.M.');
If the date has to be entered in any other format
thanother
DD-MON-YY° then we need to use the
To enter the time TO DATE function
portion of a date, the TO DATE function
must be used with a format mask indicating the time portion.
TO CHAR Syntax TOCHAR(n[,fmt])
Tumber
Purpose Converts a value of NUMBER
datatype to a value of
Conversion) CHAR datatype, using the
optional format string. fmt
must be a number format. If fmt is
omitted, n is the
converted to a char value exactly
long enough
to hold
significant digits.
Example SELECT TO CHAR(17145,
$099,999) "Char"
FROM dual;
Output Char
$017,145
INTERACTIVE SQL 41
TOCHAR Syntax TO CHAR(d[, fmt])
(date Purpose Convertss a value of DATE
conversion) the format datatype to CHAR value in
specified by the char value fmt. fmt must be a
date format. If fmt is
omitted, d is converted to a character
value in the default date
Example SELECT TO format, i.e. "DD-MON-YY".
CHAR(s order date, Month DD, YYYY')
"New Date Format" FROM
WHERE s order no sales order
Output New Date Format
=
'042453",
January 26, 1996
The above
Oracle
Oracle. These arefunctions
the Oracle just
are afew selected
from the many
functions that are most
development. They will functions that are in built
own functions using SQLserve to indicate
how commonly
Oracle functions are used in
commercial
into
Oracle function
already exists PL/SQL Oracle manuals used. Before you application
or
refer to the
that would
allow you to and to see develop
if an in your
process your datacheck
as built
required.