SQL Notes
SQL Notes
Arguments/Input:
LOWER(String)
Return type/Output
String
==========================================================================
=======UPPER=====
(1) UPPER: It is used to convert the given string into upper case
(2) Arguments/Input:
UPPER(String)
RESULT
------------
GOOD MORNING
============================================================================
=============INITCAP=====
(1) INITCAP: It will convert the first letter of the every word into upper case
(2) Arguments/Input:
INITCAP(String)
RESULT
--------------
India Is Great
=============================================================================
=====LENGTH=======
(1) LENGTH: It will give the number of characters present in the given string.
(2) Arguments/Input:
LENGTH(String)
RESULT
----------
8
============================================================================
=====SUBSTR======
(1) SUBSTR: It is used to extract the characters from the given string.
(2) Arguments/Input:
SUBSTR(String, start, length)
===================================================================================
=======
======INSTR======
(1) INSTR: It is used to search the values in the given string. It is a overloaded
method.
(2) Arguments/Input:
INSTR(String, seach)
INSTR(string, search, start, occurrence)
RESULT
----------
20
RESULT
----------
0
RESULT
----------
1
SQL> select INSTR('india great india good india', 'india', 1, 2) as RESULT from
dual;
RESULT
----------
13
Q: Write a query to find the number of characters present in the india which is
present in the below string
Ex: 'welcome to the nation india'
select LENGTH(SUBSTR('welcome to the nation india', 23, 5)) as result from dual;
RESUL
-----
india
===================================================================================
=================
====LTRIM=========
LTRIM: It is used to remove left side spaces OR characters from the given string
Arguments/Input
LTRIM(string) -> it will remove the spaces in the left side if exist.
LTRIM(string, string to remove)-> It will remove the chatacters from the left which
is mentioned in the argument2
Return type/Output
String
RESULT
--------
morning 0
select LTRIM('good morning', 'abd') as result from dual;
RESULT
------------
good morning
RESULT
--------
morning
===================================================================================
==============
=====RTRIM=====
RTRIM: It is used to remove right side spaces OR characters from the given string
Arguments/Input
RTRIM(string) -> it will remove the spaces in the right side if exist.
RTRIM(string, string to remove)-> It will remove the chatacters from the right
which is mentioned in the argument2
Return type/Output
String
Gopalkrishna, 10:51 AM
select RTRIM(' SGTEST ') as result from dual;
RESULT
------------
SGTEST
===================================================================================
=================================
===TRIM======
TRIM: It is used to remove both left & right side spaces OR characters from the
given string. It acts as LTRIM, RTRIM & TRIM
Arguments/Input
TRIM(leading <char> from <string>);-> Acts as LTRIM
TRIM(trailing <char> from <string>);-> Acts as RTRIM
TRIM(both <char> from <string>);-> Acts as TRIM
Return type/Output
String
select TRIM(leading '*' from '*****SGTESTING******') as result from dual;
RESULT
---------------
SGTESTING******
===================================================================================
======================================
=====REPLACE====
REPLACE: It is used to replace the sequencial set of charactres from the given
string
Arguments/Input
REPLACE(string, find, replacement)
Return type/Output
String
===================================================================================
==================
TRANSLATE: It is used to replace the non-sequencial set of character from the given
string
Arguments/Input
TRANSLATE(string, find, replacement)
Return type/Output
String
select TRANSLATE('ABCDEFGH', 'ADH', '123') as reuslt from dual;
REUSLT
--------
1BC2EFG3
RESULT
-----------------------
!@#$nd!@#$a !@#$s great
SQL>
SQL>
SQL> select translate('india is great', 'i', '!@#$') as result from dual;
RESULT
--------------
!nd!a !s great
===================================================================================
======================
=====LPAD=====
LPAD: It is used to print the string1 to the length specified. If the given string
is less than the length specified then it will left padded with string2
Arguments/Input
LPAD(string1, length, String2)
Return type/Output
String
RESULT
----------
ABABSGTEST
===================================================================================
===============================
====RPAD======
RPAD: It is used to print the string1 to the length specified. If the given string
is less than the length specified then it will right padded with string2
Arguments/Input
RPAD(string1, length, String2)
Return type/Output
String
==================================================================================
=====ASCII=========
ASCII: It is used display the numerical representation for the given character
Arguments/Input
ASCII(character)
Return type/Output
number
CHR: It is used display the character representation for the given number
Arguments/Input
CHR(number)
Return type/Output
character
select DECODE(deptno, '10', 'TEN', '20', 'TWENTY', '30', 'THIRTY', '40', 'FOURTY'),
DNAME, LOC from dept;
DEPTNO DNAME DE
---------- -------------- --
10 ACCOUNTING NY
20 RESEARCH DL
30 SALES CH
40 OPERATIONS BT
===================================================================================
===============================
==========NVL==============:
It stands for Null VaLue function.
Suppose If the table is having null values & you want to fill the null values with
some values then go for NVL
==================================================================================
=====ABS====
ABS: It means absolute. It is used to convert the -ve numbers into +ve numbers
input/arguments:
number
output/return type:
number
EX:
RESULT
----------
999
==================================================================================
====POWER====
POWER: It gives given number multiplied n times based on the specified values
input/arguments:
POWER(num1, num2)
output/return type:
number
EX:
RESULT
----------
16
==================================================================================
=========ROUND===
ROUND: It rounds the given values both whole number & as well as decimal values
input/arguments:
ROUND(num1)
ROUND(num1, limit)
output/return type:
number
input/arguments:
SQRT(number)
output/return type:
number
Ex:
select SQRT(9) as result from dual;
RESULT
----------
3
===================================================================================
======
====GREATEST===
GREATEST: It will give the largest number among the list of numbers
input/arguments:
GREATEST(numbers)
output/return type:
number
RESULT
----------
888
===================================================================================
==========
===LEAST=====
LEAST: It will give the smallest number among the list of numbers
input/arguments:
LEAST(numbers)
output/return type:
number
RESULT
----------
2
===================================================================================
=================
===MOD====
MOD: It will give the reminder. If divisible then returns zero otherwise returns
more than zero
input/arguments:
MOD(number1, number2)
output/return type:
number
=========================================================================
====TRUNC=====
TRUNC: It is used to get the both whole and decimal numbers
input/arguments:
TRUNC(number1)
TRUNC(number, limit)
output/return type:
number
RESULT
----------
12
SQL>
SQL>
SQL> select TRUNC(12.98765, 3) as result from dual;
RESULT
----------
12.987
===============================================================================
===REMINDER===
REMAINDER: same as mod
RESULT
----------
0
RESULT
----------
0
===SYSDATE===
SYSDATE: It will return the current system date in oracle standard date format (dd-
mon-yy)
RESULT
---------
26-FEB-21
RESULT
---------
25-FEB-21
===CURRENTDATE===
CURRENT_DATE: It will return the current system date in oracle standard date format
(dd-mon-yy)
SYSTEMDAT
---------
26-FEB-21
===LAST_DAY====
LAST_DAY: It will give the last day in that month mentioned in the input date
select LAST_DAY(sysdate) as result from dual;
RESULT
---------
28-FEB-21
====MONTHS_BETWEEN==
MONTHS_BETWEEN: It will returns the difference between 2 given dates in month
format
====NEXT_DAY===
NEXT_DAY: It will return the next occurrence of the given day
===ADD_MONTHS===
ADD_MONTHS: It is used to add the number of mnths to the given date
RESULT
---------
26-JUL-21
===CURRENT_TIMESTAMP==
CURRENT_TIMESTAMP: It will return the time stamp
TIMESTAMP
---------------------------------------------------------------------------
26-FEB-21 10.06.38.551000 +05:30
===SYSTIMESTAMP===
TIMESTAMP
---------------------------------------------------------------------------
26-FEB-21 10.07.39.898000 +05:30
====SESSIONTIMEZONE===
SESSIONTIMEZONE: it returns the time zone
ZONE
---------------------------------------------------------------------------
+05:30
===LOCALTIMESTAMP===
LOCALTIMESTAMP: It will provide the timestamp with the zone details
LOCALTIMESTAMP
---------------------------------------------------------------------------
26-FEB-21 10.09.51.810000
Gopalkrishna, 10:23 AM
===TO_DATE==
select to_date(sysdate, 'dd-mon-yy') as result from dual;
RESULT
---------
26-FEB-21
=========================================================================
CONVERSION FUNCTIONS
REUSLT
---------
01-JAN-21
RESULT
--------------------------
26-february -20 12:02:00
RESULT
----------
9999
===================================================================================
============
Example
Name (varchar2(10)) Age(number(2)) City(varchar2(15)) Gender(char(1))
==CREATE====
create table EXAMPLE(
NAME varchar2(15),
AGE number(2),
CITY varchar2(15),
GENDER char(1));
Table altered.