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

SQL Notes

The document describes various string functions in Oracle SQL including LOWER, UPPER, INITCAP, LENGTH, SUBSTR, INSTR, LTRIM, RTRIM, TRIM, REPLACE, TRANSLATE, LPAD, RPAD, ASCII, CHR and DECODE. These functions allow manipulating and extracting data from strings, converting case, finding lengths and positions, padding strings, and conditional logic. Examples are provided for each function to demonstrate their usage.

Uploaded by

Nigar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

SQL Notes

The document describes various string functions in Oracle SQL including LOWER, UPPER, INITCAP, LENGTH, SUBSTR, INSTR, LTRIM, RTRIM, TRIM, REPLACE, TRANSLATE, LPAD, RPAD, ASCII, CHR and DECODE. These functions allow manipulating and extracting data from strings, converting case, finding lengths and positions, padding strings, and conditional logic. Examples are provided for each function to demonstrate their usage.

Uploaded by

Nigar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

(1) LOWER: It is used to convert the given string into lower case

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)

(3) Return type/Output


StringSQL> select UPPER('good morning') as RESULT from dual;

RESULT
------------
GOOD MORNING

============================================================================
=============INITCAP=====
(1) INITCAP: It will convert the first letter of the every word into upper case

(2) Arguments/Input:
INITCAP(String)

(3) Return type/Output


String
> select INITCAP('india is great') as RESULT from dual;

RESULT
--------------
India Is Great

=============================================================================

=====LENGTH=======
(1) LENGTH: It will give the number of characters present in the given string.

(2) Arguments/Input:
LENGTH(String)

(3) Return type/Output


number

select LENGTH('india @1') as RESULT from dual;

RESULT
----------
8

============================================================================

=====SUBSTR======

(1) SUBSTR: It is used to extract the characters from the given string.
(2) Arguments/Input:
SUBSTR(String, start, length)

(3) Return type/Output


String

select SUBSTR('good morining india', 1, 4) as RESULT from dual;


RESU
----
good
--------------------------------------------------------------------
select SUBSTR('good morning india', 14, 5) as RESULT from dual;
RESUL
-----
india

===================================================================================
=======

======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)

(3) Return type/Output


number
0 means search is not found
>0 means the search is found in the given index

select INSTR('the garden city is bangalore', 'bangalore') as result from dual;

RESULT
----------
20

select INSTR('the garden city is bangalore', 'mangalore') as result from dual;

RESULT
----------
0

select INSTR('bangalore city is bangalore', 'bangalore') as result from dual;

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'

-> Extract india


-> check of characters present in india

select LENGTH(SUBSTR('welcome to the nation india', 23, 5)) as result from dual;

Q: Write a query to extract india from the given string.


Ex: 'welcome to the nation india'

select SUBSTR('welcome to the nation india', instr('welcome to the nation india',


'india'), 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

select LTRIM(' india ') as result from dual;


RESULT
---------------
india

select LTRIM('good morning', 'good') as result from dual;


RESULT
--------
morning

select LTRIM(' good morning', 'good') as result from dual;


RESULT
-------------
good morning

select LTRIM('good morning', 'dog') as result from dual;

RESULT
--------
morning 0
select LTRIM('good morning', 'abd') as result from dual;

RESULT
------------
good morning

select LTRIM('good morning', 'god') as result from dual;

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

guest:7f69035d-dd2e-4e51-bbd6-25211469d04c joined this conversation

Gopalkrishna, 10:51 AM
select RTRIM(' SGTEST ') as result from dual;
RESULT
------------
SGTEST

select RTRIM('good morning', 'good') as result from dual;


RESULT
-----------
good mornin

select RTRIM('xxXyyYzzZx', 'X') as result from dual;


RESULT
----------
xxXyyYzzZx

===================================================================================
=================================
===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******

select TRIM(trailing '*' from '*****SGTESTING******') as result from dual;


RESULT
--------------
*****SGTESTING

select TRIM(both '*' 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

select REPLACE('Blore is garden city', 'is', 'was') as result from dual;


RESULT
---------------------
Blore was garden city

select REPLACE('good morning', 'day', 'XXXXXXXX') as result from dual;


RESULT
------------
good morning

select REPLACE('good morningDAY', 'DAY') as result from dual;


RESULT
------------
good morning

===================================================================================
==================
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

1. replace for replacing sequential characters


1. translate for replacing non-sequential characters

2. in replace 3rd argument is optional. By default it will take blank


2. in translate 3rd argument is mandatory

3. in replace, you can replace the characters/words/sentense


3. in translate, you can replace character by characters

select REPLACE('india is great', 'i', '!@#$') as result from dual;

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

select LPAD('india', 5, '*') as reuslt from dual;


REUSL
-----
india

select LPAD('india', 10, '*') as reuslt from dual;


REUSLT
----------
*****india

select LPAD('india', 10, 'BLORE') as reuslt from dual;


REUSLT
----------
BLOREindia
select LPAD('india', 20, 'BLORE') as reuslt from dual;
REUSLT
--------------------
BLOREBLOREBLOREindia

select LPAD('SGTEST', 10, 'AB') as result from dual;

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

select RPAD('SGTEST', 6, '*') as result from dual;


RESULT
------
SGTEST

select RPAD('SGTEST', 10, '*') as result from dual;


RESULT
----------
SGTEST****

==================================================================================
=====ASCII=========

ASCII: It is used display the numerical representation for the given character

Arguments/Input
ASCII(character)

Return type/Output
number

select ASCII('A') as result from dual;


RESULT
----------
65

select ASCII('a') as result from dual;


RESULT
----------
97

select ASCII(' ') as result from dual;


RESULT
----------
32

select ASCII('$') as result from dual;


RESULT
----------
36
==================================================================================
======CHR=====

CHR: It is used display the character representation for the given number

Arguments/Input
CHR(number)

Return type/Output
character

select CHR(36) as result from dual;


R
-
$

select CHR(64) as result from dual;


R
-
@

select CHR(65) as result from dual;


R
-
A
===================================================================================
========
====DECODE====

DECODE: It is a conditional statement in SQL


It acts as If else statement in SQL

select DECODE(deptno, '10', 'TEN', '20', 'TWENTY', '30', 'THIRTY', '40', 'FOURTY'),
DNAME, LOC from dept;

DECODE DNAME LOC


------ -------------- -------------
TEN ACCOUNTING NEW YORK
TWENTY RESEARCH DALLAS
THIRTY SALES CHICAGO
FOURTY OPERATIONS BOSTON
select deptno, dname, DECODE(LOC, 'NEW YORK', 'NY', 'DALLAS', 'DL', 'CHICAGO',
'CH', 'BOSTON', 'BT') 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

select ename, job, sal, NVL(COMM, 1) as COMM from emp;

==================================================================================
=====ABS====

ABS: It means absolute. It is used to convert the -ve numbers into +ve numbers

input/arguments:
number

output/return type:
number

EX:

select ABS(-999) as result from dual;

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:

select POWER(2, 4) as result from dual;

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

select ROUND(12.675) as result from dual;


RESULT
----------
13

select ROUND(12.675, 2) as result from dual;


RESULT
----------
12.68
SQRT: It will give the square root for the given 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

select GREATEST(100, 2, 99, 888, 65) as result from dual;

RESULT
----------
888

===================================================================================
==========
===LEAST=====
LEAST: It will give the smallest number among the list of numbers

input/arguments:
LEAST(numbers)

output/return type:
number

select LEAST(100, 2, 99, 888, 65) as result from dual;

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

select MOD(8, 2) as result from dual;


RESULT
----------
0

select MOD(9, 3) as result from dual;


RESULT
----------
0

select MOD(9, 2) as result from dual;


RESULT
----------
1

=========================================================================
====TRUNC=====
TRUNC: It is used to get the both whole and decimal numbers

input/arguments:
TRUNC(number1)
TRUNC(number, limit)

output/return type:
number

select TRUNC(12.5678) as result from dual;

RESULT
----------
12
SQL>
SQL>
SQL> select TRUNC(12.98765, 3) as result from dual;

RESULT
----------
12.987
===============================================================================
===REMINDER===
REMAINDER: same as mod

select REMAINDER(9, 3) as result from dual;

RESULT
----------
0

SQL> select REMAINDER(27, 3) as result from dual;

RESULT
----------
0

===SYSDATE===
SYSDATE: It will return the current system date in oracle standard date format (dd-
mon-yy)

select SYSDATE as result from dual;

RESULT
---------
26-FEB-21

Q: Display yesterday's date?


select SYSDATE-1 as result from dual;

RESULT
---------
25-FEB-21

===CURRENTDATE===
CURRENT_DATE: It will return the current system date in oracle standard date format
(dd-mon-yy)

select CURRENT_DATE as systemDate from dual;

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

select LAST_DAY('01-FEB-2020') as result from dual;


RESULT
---------
29-FEB-20

====MONTHS_BETWEEN==
MONTHS_BETWEEN: It will returns the difference between 2 given dates in month
format

select MONTHS_BETWEEN(sysdate, '15-aug-1947') as result from dual;


RESULT
----------
882.368222

====NEXT_DAY===
NEXT_DAY: It will return the next occurrence of the given day

select NEXT_DAY(sysdate, 'FRI') as result from dual;


RESULT
---------
05-MAR-21

select NEXT_DAY(sysdate, 'MONDAY') as result from dual;


RESULT
---------
01-MAR-21

===ADD_MONTHS===
ADD_MONTHS: It is used to add the number of mnths to the given date

select ADD_MONTHS(sysdate, 5) as result from dual;

RESULT
---------
26-JUL-21

===CURRENT_TIMESTAMP==
CURRENT_TIMESTAMP: It will return the time stamp

select CURRENT_TIMESTAMP as timestamp from dual;

TIMESTAMP
---------------------------------------------------------------------------
26-FEB-21 10.06.38.551000 +05:30

===SYSTIMESTAMP===

SYSTIMESTAMP: It will return the time stamp

select SYSTIMESTAMP as timestamp from dual;

TIMESTAMP
---------------------------------------------------------------------------
26-FEB-21 10.07.39.898000 +05:30

====SESSIONTIMEZONE===
SESSIONTIMEZONE: it returns the time zone

select SESSIONTIMEZONE zone from dual;

ZONE
---------------------------------------------------------------------------
+05:30

===LOCALTIMESTAMP===
LOCALTIMESTAMP: It will provide the timestamp with the zone details

select LOCALTIMESTAMP localtimestamp from dual;

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

select to_date('2021-FEB-01', 'yyyy-mon-dd') as reuslt from dual;


REUSLT
---------
01-FEB-21

select TO_DATE('JAN-01-21', 'mon-dd-yy') as reuslt from dual;

REUSLT
---------
01-JAN-21

select TO_CHAR(sysdate, 'month-year') as result from dual;


RESULT
-----------------------------
february -twenty twenty-one

select TO_CHAR(sysdate, 'day-mon-year') as result from dual;


RESULT
--------------------------------
friday -feb-twenty twenty-one

select TO_CHAR(to_date('26-FEB-20','dd-mon-yy'), 'dd-month-yy hh12:mm:ss') as


result from dual
SQL> /

RESULT
--------------------------
26-february -20 12:02:00

select TO_NUMBER('9999') as result from dual;

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));

rename example to sample;

Add new column to the table:


syntax:
Alter table <tableName> add (<newColName> <datatype> <size>, ....);

alter table SAMPLE add (DOB date, PINCODE number(6));

modify the column datatype:


syntax:
Alter table <tableName> modify (<colName> <newDatatype> <newSize>)

alter table SAMPLE modify (GENDER varchar2(6));

Table altered.

delete the column from the table:


syntax:
(i)alter table <tableName> drop column <colName>;
(ii)alter table <tableName> drop (<colName1>,<col2Name2>);

alter table SAMPLE drop column PINCODE;

alter table SAMPLE drop (GENDER, DOB);

(d) Rename the column name:


syntax:
alter table <tableName> rename column <oldColName> to <newColName>;

alter table SAMPLE rename column NAME to PersonName;

truncate table sample;


insert into SAMPLE values('USER1', 21, 'RAICHUR', '01-JAN-95')

You might also like