0% found this document useful (0 votes)
9 views5 pages

4 TH Experiment

The document provides an overview of various SQL functions including conversion functions (TO_CHAR, TO_NUMBER, TO_DATE), string functions (CONCAT, LPAD, RPAD, LTRIM, RTRIM, LOWER, UPPER, INITCAP, LENGTH, SUBSTR), and date functions (SYSDATE, NEXT_DAY, ADD_MONTHS, LAST_DAY, MONTHS_BETWEEN, LEAST, GREATEST, TRUNC). It includes examples demonstrating how to use these functions with sample data from an 'emp' table. The document serves as a reference for performing data manipulation and formatting in SQL.

Uploaded by

likhithakonda28
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)
9 views5 pages

4 TH Experiment

The document provides an overview of various SQL functions including conversion functions (TO_CHAR, TO_NUMBER, TO_DATE), string functions (CONCAT, LPAD, RPAD, LTRIM, RTRIM, LOWER, UPPER, INITCAP, LENGTH, SUBSTR), and date functions (SYSDATE, NEXT_DAY, ADD_MONTHS, LAST_DAY, MONTHS_BETWEEN, LEAST, GREATEST, TRUNC). It includes examples demonstrating how to use these functions with sample data from an 'emp' table. The document serves as a reference for performing data manipulation and formatting in SQL.

Uploaded by

likhithakonda28
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/ 5

4.

QUERIES USING CONVERSION FUNCTIONS (TO_CHAR, TO_NUMBER AND TO_DATE),


STRING FUNCTIONS (CONCATENATION, LPAD, RPAD, LTRIM, RTRIM, LOWER, UPPER,
INITCAP, LENGTH, SUBSTR AND INSTR), DATE FUNCTIONS (SYSDATE, NEXT_DAY,
ADD_MONTHS, LAST_DAY, MONTHS_BETWEEN, LEAST,
GREATEST, TRUNC, ROUND, TO_CHAR)

SQL> select *from emp;


ENO ENAME SALARY LOC
101 ali 15000 vja
102 haji 20000 hyd
103 mohammad 42000 vja
104 ravi 23000 gnt
105 irfath 50000 hyd

a) Conversion Functions:
1. to_char: to_char is used to convert the attribute values to char.
SQL> select to_char(salary,'$99999.99') from emp;
TO_CHAR(SALARY)
$15000.00
$20000.00
$42000.00
$23000.00
$50000.00
SQL> SELECT TO_CHAR (123.4567, '99999.9') FROM DUAL;
TO_CHAR (
123.5
SQL> SELECT TO_CHAR(123.4567, '99999.99') FROM DUAL;
TO_CHAR(1
123.46

SQL> SELECT TO_CHAR(SYSDATE, 'YYYY/MM/DD') FROM DUAL;


TO_CHAR(SY 2021/07/09

SQL> SELECT TO_CHAR (SYSDATE, 'DD/MM/YYYY') FROM DUAL;


TO_CHAR(SY 09/07/2021

SQL> SELECT TO_CHAR (23, '000099') FROM DUAL;


TO_CHAR 000023

SQL> SELECT TO_CHAR (23, '0000999') FROM DUAL;


TO_CHAR( 0000023

2. to_number: to_number is used to convert the attribute value to number.

SQL> SELECT TO_NUMBER('1210.73', '9999.99') FROM DUAL;


TO_NUMBER('1210.73','9999.99') 1210.73
3. to_date: to_date is used for convert and display the attribute values as
date. SQL> select to_date('01-01-2020', 'MM-DD-YYYY') from dual;

TO_DATE(' 01-JAN-20

b) String functions:
1. Concatenation: CONCAT is used to add two attribute values such as
string.

SQL> select concat (eno, loc) from emp;


CONCAT(ENO,LOC)
101vja 102hyd 103vja 104gnt 105hyd

2. lpad: LPAD() function is used to padding the left side of a string with a
specific set of characters.

SQL> select lpad(ename,10,'*') from emp;


LPAD(ENAME,10,'*')
*******ali
******haji
**mohammad
******ravi
****irfath
3. rpad: RPAD() function is used to padding the right side of a string with
a specific set of characters.

SQL> select rpad(ename,10,'*') from emp;


RPAD(ENAME,10,'*')
ali******* haji****** mohammad** ravi****** irfath****

ltrim: LTRIM() function is used to remove all specified characters from


the left end side of a string

SQL> select ltrim('******hi********','*') from dual;


LTRIM('*** hi********
5. rtrim: RTRIM() function is used to remove all specified characters
from the left end side of a string

SQL> select rtrim('******hi********','*') from dual;


RTRIM('*
******hi
6. lower: lower() function is used to convert the attribute value in to lower
case.

SQL> select lower(ename) from emp;


LOWER(ENAM
ali haji
mohammad ravi
irfath
7. upper: upper() function is used to convert the attribute values in to
upper case.

SQL> select upper(ename) from emp;


UPPER(ENAM ALI
HAJI
MOHAMMAD RAVI
IRFATH

8. initcap: initcap() is used to convert the attribute values first character in


capital letter.

SQL> select initcap (ename) from emp;


INITCAP(EN
Ali Haji
Mohammad Ravi
Irfath
9. length: length() function is used to calculate the length of the given
attribute.

SQL> select ename,length(ename) from emp;


ENAME LENGTH(ENAME)
ali 3
haji 4
mohammad 8
ravi 4
irfath 6
10. substr:substr() function is used to find the substring of the given
attribute value. It retuns size-1 of the given string/ attribute as a sub string.

SQL> select ename, substr(ename,4) from emp;


ENAME SUBSTR(ENAME,4)
ali
haji i
mohammad ammad ravi i
irfath ath

c) Date functions:
1. Sysdate(): sysdate() function returns the current system date.

SQL> select sysdate from dual;


SYSDATE 28-APR-21
2. next_day(); it reurns the date of next coming day .

SQL> select next_day(sysdate,'sunday') from dual;


NEXT_DAY( 02-MAY-21
3. add_months(): it returns the next date after adding number of months in the orguments.

SQL> select add_months(sysdate,5) from dual;


ADD_MONTH 28-SEP-21
4. last_day(): The LAST_DAY() function takes a date value as argument and returns the last day of month in that da

SQL> select last_day(sysdate) from dual;


LAST_DAY( 30-APR-21
SQL> select last_day('02-FEB-2020') from dual;
LAST_DAY(

29-FEB-20
5. months_between(): it returns the numbers of months between given two dates.

SQL> select months_between('02-feb-2021','02-feb-2020') from dual;


MONTHS_BETWEEN('02-FEB-2021','02-FEB-2020') 12
SQL> select months_between(sysdate,'02-feb-2020') from dual;
MONTHS_BETWEEN(SYSDATE,'02-FEB-2020')
14.8600769
6. least(): it retuns least value from the given argument or attributes.

SQL> select least(300,450,100,440) from dual;


LEAST(300,450,100,440) 100
7. greatest(): it returns maximum values from the given arguments or attributes in the relation.

SQL> select greatest(300,450,100,440) from dual;


GREATEST(300,450,100,440) 450
8. trunc(): The TRUNC() function returns a DATE value truncated to a specified unit.

SQL> select trunc(sysdate,'mm') from dual;


TRUNC(SYS 01-APR-21
SQL> select trunc(sysdate,'yyyy') from dual;
TRUNC(SYS

You might also like