sql functions and operators

17
SQL Functions & Operators CS2258 Database Management Systems Lab R. Mohan Kumar Assistant Professor / CSE Saranathan College of Engineering

Upload: mohan-kumarr

Post on 15-Jul-2015

129 views

Category:

Technology


2 download

TRANSCRIPT

SQL Functions &

Operators

CS2258 – Database Management Systems Lab

R. Mohan Kumar

Assistant Professor / CSE

Saranathan College of Engineering

SQL Functions

Numeric Functions

Date Functions

Text Functions

CS2258 – Database Management Systems Lab

Syntax

select <function_name> from dual;

2

Numeric Functions

CS2258 – Database Management Systems Lab

FUNCTION NAME DESCRIPTION

ABS(x) Absolute value of the number “x”

CEIL(x) Integer value that is greater than (or) equal to “x”

FLOOR(x) Integer value that is lesser than (or) equal to “x”

TRUNC(x,y) Truncate values of “x” upto “y” decimal places.

ROUND(x,y) Rounded off value of “x” upto “y” decimal places.

1 of 5

3

Numeric Functions

CS2258 – Database Management Systems Lab

FUNCTION NAME DESCRIPTION

MOD(x,y) Returns the result of X % Y (remainder)

POWER(x,y) Returns the value of X ^ Y

SQRT(x) Returns the square root value of X

LOG(x) Returns the Base 10 logarithm of X

SIGN(x) 1 if X is POSITIVE ; -1 if X is NEGATIVE ; 0 if X is ZERO

2 of 5

4

Numeric Functions

CS2258 – Database Management Systems Lab

FUNCTION NAME DESCRIPTION

SIN(x) Returns the Sine value of X

COS(x) Returns the Cosine value of X

TAN(x) Returns the Tangent value of X

EXP(x) E raised to X exponent

LN(x) Returns the Natural Logarithm of X

3 of 5

5

Numeric Functions

CS2258 – Database Management Systems Lab

FUNCTION NAME DESCRIPTION

MAX()

It selects the MAXIMUM value in the specified column.

Syntax:

Select MAX(<col_name>) from <table_name>;

MIN()

It selects the MINIMUM value in the specified column.

Syntax:

Select MIN(<col_name>) from <table_name>;

MEDIAN()

It returns the MEDIAN of all values in the given

column.

Syntax:

Select MEDIAN(<col_name>) from <table_name>;

4 of 5

6

Numeric Functions

CS2258 – Database Management Systems Lab

FUNCTION NAME DESCRIPTION

AVG()

It returns the average of all values in the given column.

Syntax:

Select AVG(<col_name>) from <table_name>;

SUM()

It returns the sum of all values in the given column.

Syntax:

Select SUM(<col_name>) from <table_name>;

COUNT()

It returns the no. of rows in the given column.

Syntax:

Select COUNT(<col_name>) from <table_name>;

5 of 5

7

Date Functions

FUNCTION NAME DESCRIPTION

SYSDATE Returns the systems current date.

ADD_MONTHS(date,n) Returns a date value after adding in months

to the “date”.

MONTHS_BETWEEN(x1,x2) Returns the number of months between dates

x1 and x2.

NEXTDAY(x, weekday) Returns the next date of the “weekday” on or

after “x” occurs.

LAST_DAY(x) Returns the last date of the particular month.

CS2258 – Database Management Systems Lab

1 of 2

8

Date Functions

FUNCTION NAME DESCRIPTION

SysTimeStamp Returns the system date in TIMESTAMP

format.

To_char(date,format)

Returns the date value in given format.

Syntax:

Select to_char(sysdate,’mm/dd/yy’)

from dual;

CS2258 – Database Management Systems Lab

2 of 2

9

String / Text Functions

FUNCTION NAME DESCRIPTION

UPPER(x) It converts the string ‘X’ to UPPER case

LOWER(x) It converts the string ‘X’ to LOWER case

RTRIM(x,y)

It returns the string after trimming all

occurrence of sub string ‘y’ from right side of

the input string ‘X’.

LTRIM(x,y)

It returns the string after trimming all

occurrence of sub string ‘y’ from left side of

the input string ‘X’.

LENGTH(x) Returns the length of the input string ‘X’

CS2258 – Database Management Systems Lab

1 of 2

10

String / Text Functions

FUNCTION NAME DESCRIPTION

INITCAP(x) It converts the first character of the input

string to UPPER case.

SUBSTR(x,m,n)

Returns the substring of given string starting

from position integer ‘m’ and length integer

‘n’.

TRANSLATE(x,new,old) It converts the string by substituting the new

string in the place of old string.

ASCII(c) Returns the ASCII value of the character ‘c’.

CHR(number) Returns the character for a ASCII value

‘number’.

CS2258 – Database Management Systems Lab

2 of 2

11

Operators

Arithmetic Operators

+ , - , * , /

Comparison Operators

= , != , <, <=, >, >=

Like, In, Between And , Is Null

Logical Operators

OR, AND, NOT

CS2258 – Database Management Systems Lab 12

Operators usage

Arithmetic Operator

select 2 + 3 from dual;

select 10 - 5 from dual;

select salary + allowance from worker;

salary & allowance are the attributes of worker table.

update customer set balance = balance + balance * 0.075;

CS2258 – Database Management Systems Lab 13

Operators usage

Comparison Operator

select * from customer where amount = 10000;

select * from customer where amount < 500;

select * from customer where age > 68;

update customer set interest_rate = 0.0925

where age >= 68;

delete from customer where balance <= 50;

CS2258 – Database Management Systems Lab 14

Operators usage

Comparison Operator

select * from customer where cname LIKE ‘a%;

select * from customer where cname LIKE ‘%er;

select * from customer where cname IN (value1,value2,…);

delete from customer where balance BETWEEN 0 AND 50;

select * from customer where phoneno IS NULL;

CS2258 – Database Management Systems Lab 15

Operators usage

Logical Operator

select * from customer where cname=‘john’ OR

cname=‘joshy’ ;

select * from customer where fname=‘joe’ AND

lname=‘parker’ ;

select * from customer where phoneno IS NOT NULL;

CS2258 – Database Management Systems Lab 16

17

Browse for this material

www.sara-eclass.blogspot.in

CS2258 – Database Management Systems Lab