0% found this document useful (0 votes)
4 views

Oracle_4

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Oracle_4

Uploaded by

mohammed.shohel9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

C) Date functions:

i) Sysdate:
It displays the system date and time
We can use date functions by adding and subtracting values from a date function.

Ex: select sysdate from dual;


select sysdate-1 from dual;

ii) Current date:

The current date function returns the current date in the time zone of current sql session as set by the
alter session command

Syn: select current_sate from dual;

Note: we have to alter the session and time zone to use current_date

iii) Add_months:

it is used to add or subtract months from given date.

Ex: select add_months (sysdate,3) from dual;


select add_months (sysdate,-3) from dual;

iV) Months-between:

It is used to display the number of the months between two dates.

Ex: select months-between ( sysdate, ’15-sep-19’ ) from dual;


select months-between (sysdate, hiredate) from dual;

V. Next_day:

To display next_day based on the specified day


Ex: select next_day (sysdate, ‘monday’) from dual;
select next_day (sysdate, ‘thrusday’) from dual;

Vi. Last_day:

To display the last day of the given month


Ex: select last_day (sysdate ) from dual;
D) Conversion functions:
i) To_char:

this function converts a number or date to string


Syn: to_char( value, desired format )

To_char with numbers:

To_char (1210.73, ‘9999.9’)


O/P: 1210.7

To_char (-1210.73, ‘9999.9’)


O/P: -1210.7

To_char (1210.73, ‘9999.99’)


O/P: 1210.73

To_char (1210.73, ‘9999.00’)


O/P: 1210.73

To_char (21, ‘000099’)


O/P: 000021

To_char ( 1210.234, ‘$999.99’)


O/P: $123.23

To_char with dates:

It converts the system date format to user specified format.

To-char (sysdate, ‘yyyy/mm/dd’)


O/P: 2010/03/10

To_char (sysdate, ‘month dd,yyyy’)


O/P: July 09,2003

To_char (sysdate, ‘fmmonth,yyyy’)


O/P: july9,2003

To_char (sysdate, ‘mon ddth, yyyy’)


O/P: Jul 9th,2003
To_char (sysdate, ‘fmmonddth,yy)
O/P: jul 9th, 03

Note: In the above examples fm means format_mask parameter. It means the zeros and blanks are
suppressed

Parameter/Format Explanation
yyyy 4 digit year
yyy or yy or y last 3 or 2 or 1
mon abbreviated month (jan-dec)
month name of the month (January-December)
day name of day
dd day of month(1-31)
ddd day of year (1-31)
hh hour of day (1-12)
hh24 hour of day (0-23)
mi minutes (0-59)
ss (0-59)
year to write the complete year name

ii) to_date:

it is used to convert any other date format to system default format.

Ex: to_date (‘2003/07/09’, ‘yyyy/mm/dd’)


O/P: sysdate format à 03-july-09

To_date (‘070903’, ‘mmddyy’)


O/P: sysdate format à 09-jul-03

To_date (‘20020315’, ‘yyyymmdd’) from dual;


O/P: sysdate format à 15-mar-02

To_date (‘2015/05/15 8:30:24, yyyy/mm/ddhh:mi:ss’)


O/P: 15-may-15

iii) to_number:

it is used to convert the string to number .


Ex: to_number ( '$123.23', '$999.99' )
O/P: 123.23
E) General functions:
i)User:
Select user from dual;

ii) Uid:
Select uid from dual;

iii) NVL:
It is used to replace the null values of a column with given value.
Syn: nvl (col_name, replacement value)
Ex: select nvl (sal,10000) from emp;
The above command replaces the null values of sal column with values of 10000

iv)NVL2:
It is extended functionality of nvl function.
It allows you to substitute a value when a null is encountered as well as not null values is encountered

Syn: nvl2 (col_name, value_if_not_null, value_if_null);

Select nvl (sal, 10000, 20000) from emp;


The above command replaces null values sal column with 20000 and non-null value with 10000

V)Null if:
The null-if function compares expr1 and expr2.
If expr1 and expr2 are equal it returns null, It expr1 and expr2 not equal it returns expr1

Syn: nullif (expr1, expr2)

Ex: select nullif (10,10) from dual;


O/P: null
Select nullif (20,10) from dual;
O/P: 20

Vi) Coalesce:
The coalesce function returns the first non-null expression in the list. If all expressions evaluate to null
then the coalesce function will return null.
Syn: coalesce (expr1,expr2,…..exprn);
Ex: coalesce (empname1, empname2, empname3) from emp;
In the above example if empname is not null it will display empname1
If empname1 is null, if empname2 is not-null it will display empname2
If empname1 and empname2 is null, if empname3 is not null it will display empname3;
If all nulls it will display null only.
Vii) Case:

Case statement used to implement logic as if-then-else statement.


there are two types of case expressions.

a)Simple case expression


b)Searched case expression

a) Simple case expression:

Syn: case expression when comparison_expr then statement


when comparison_expr then statement
when comparison_expr then statement

.
.
.

else return_expr end

In the simple case expression oracle database searches for the comparison expression then return the
corresponding statement.

If none of the comparison expression matches then it returns else statement.

The maximum number of argument in a case expression is 255.

Note: If we don’t specify the else condition it returns null.

Example:
Select cust-last-name, case credit-limit
When 1000 then low
When 5000 then high
Else medium END
From emp;

b) searched case expression:

Case when condition then statement


When condition then statement
.
.
Else statement
End
Here it searches of condition that is true and returns the statement.
Example: select case when empno in (1234,1235) and sal > 5000
Then ‘first grade’
When empno in (1236,1237) and sal< 5000
Then ‘second grade’
Else ‘third grade’
end;

Difference between simple case and search case:

The simple case performance a simple equality check of expression against each of the when options.
The searched case evaluates the conditions independently under each of the when options, more
complex conditions can be implemented with search case
A searched case can combine multiple tests using and.

Viii) Decode:

It has the functionality of if-then-else statement using decode function we can substitute value with
another value.
Syn: Decode ( col_name, value, result, value, result, value,….. default value)

Here default value used to replace the other values with default value, If we don’t specify the default
value it will replaces the rest of values as null

Ex: select decode (S-area, ‘bellandur’, ‘karnataka’, ‘rct’, ‘a.p’, ‘chennai’, ‘t.n’ , ‘rest of india’) from
table_name;
The above function replaces s-area column where Bellandur with Karnataka, Rct with A.P , Chennai with
T.N and other values with rest of India

Difference between decode and case function:

Decode and case statements both give values based on condition, like it-then-else

àIn case function we can use logical all operators including =,<>,>,< and all other operators
But in decode function we cannot use any operators we can use equity check condition

àCase function can be used with subqueries in search table from


In decode function we can’t use any within queries

àCase expects datatype consistency and decode does not expect datatype consistency.

You might also like