DBMS Unit 3
DBMS Unit 3
DBMS Unit 3
com
UNIT-III
QUERIES, CONSTRAINTS, TRIGGERS
Introduction to SQL: SQL stands for Structured Query Language. It is the most widely used
commercial relational database language. There are numerous versions of SQL. The original SQL
version was developed at IBM‟s San Jose Research Laboratory, California in 1970. This was originally
called as Sequel Language of system R project in the early 1970‟s. The sequel was renamed as SQL
during the years 1974 to 1979. The SQL is a combination of relational algebra and relational calculus.
This was a run under the DOS operating system in 1981. The SQL language was announced as the
MVS version, called DB2 in 1983.
In 1986, the ANSI/ISO published a SQL standard, called SQL-86. IBM published its own
corporate SQL standard, called the System Application Architecture database interface (SAA-SQL) in
1987. An extended standard of SQL was published in 1989 by ANSI/ISO. The Next versions of
ANSI/ISO are SQL-92,SQL-94,SQL-96, and SQL-99. Some Other Organizations developed Relational
Data Base Software‟s, called INGRES, SYBASE etc.
In the DBMS, SQL can create the tables, translate user requests, maintain the data dictionary,
maintain the system catalog, update and maintain the tables, establish security and carry out backup and
recovery procedures.
1
Jntuk396.blogspot.com
Jntuk396.blogspot.com
2. To define the data structures and basic operations for designing, accessing, maintaining, controlling
and protecting an SQL database.
3. To provide portability of database definitions (Tables) and application modules between different
DBMS.
4. To specify both Level 1 and Level 2 standards. I.e., simple and complex queries.
5. To provides an initial standards for handling integrity, transaction management, user-defined
functions, join operators, set operators, etc.
SQL Benefits or Advantages : The benefits of relational language include the following. They are
1. Reduced training costs: This reduces the training costs in organization.
2. Productivity: The user can learn SQL language thoroughly and become proficient. The
programmers also write the programs and quickly maintain existing programs in SQL.
3. Application Portability: Applications can be easily moved from machine to machine and can
develop the standards.
4. Application longevity: The old applications developed in SQL old versions can be developed using
the advanced SQL do not taking a long time.
5. Reduced dependence on a single vendor: The learners can get assistance, training and educational
services from the vendors. Because, the SQL is easy language. So user does not depend on single
person. [i.e., more vendors are available to give the assistance].
6. Cross-system communication: Different DBMSs and application programs can more easily
communicate and co-operate in managing data and processing user programs.
we will present a number of sample queries using the following table definitions:
Sailors( sid: integer, sname: string, rating: integer, age: real)
Boats( bid: integer, bname: string, color: string)
Reserves (sid: integer, bid: integer, day: date)
2
Jntuk396.blogspot.com
Jntuk396.blogspot.com
SQL>SELECT S.sid, S.sname, S.rating, S.age FROM Sailors AS S WHERE S.rating > 7
This query uses the optional keyword AS to introduce a range variable. Incidentally, when we
want to retrieve all columns, as in this query, SQL provides convenient shorthand: we can simply write
SELECT *. This notation is useful for interactive querying, but it is poor style for queries that are
intended to be reused and maintained because the schema of the result is not clear from the query itself;
we have to refer to the schema of the underlying Sailors table.
3
Jntuk396.blogspot.com
Jntuk396.blogspot.com
check if a set is empty). IN and EXISTS can be prefixed by NOT, with the obvious modification to their
meaning.
Ex: Find the names of sailors who have reserved a red or a green boat
SQL>SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid =
B.bid AND B.color = „red‟ UNION SELECT S1.sname FROM Sailors S1, Boats B1, Reserves R1
WHERE S1.sid = R1.sid AND R1.bid = B1.bid AND B1.color = 'green';
Ex: Find the names of sailor's who have reserved both a red and a green boats.
SQL>SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid =
B.bid AND B.color = „red‟ INTERSECT SELECT S1.sname FROM Sailors S1, Boats B1, Reserves
R1 WHERE S1.sid = R1.sid AND R1.bid = B1.bid AND B1.color = 'green';
Ex: Find the sids of all sailor's who have reserved red boats but not green boats.
SQL>SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid
AND B.color = „red‟ EXCEPT SELECT S1.sid FROM Sailors S1, Boats B1, Reserves R1 WHERE
S1.sid = R1.sid AND R1.bid = B1.bid AND B1.color = 'green';
Sailors 22, 64, and 31 have reserved red boats. Sailors 22, 74, and 31 have reserved green boats. Hence,
the answer contains just the sid 64.
NESTED QUERIES:
A Sub query or Inner query or a Nested query is a query within another SQL query and embedded
within the WHERE clause.
A sub query is used to return data that will be used in the main query as a condition to further restrict
the data to be retrieved.
Sub queries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
There are a few rules that sub queries must follow
Sub queries must be enclosed within parentheses.
A sub query can have only one column in the SELECT clause, unless multiple columns are in
the main query for the sub query to compare its selected columns.
An ORDER BY command cannot be used in a sub query, although the main query can use an
ORDER BY. The GROUP BY command can be used to perform the same function as the
ORDER BY in a sub query.
Sub queries that return more than one row can only be used with multiple value operators such
as the IN operator.
A sub query cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a sub query. However, the BETWEEN operator
can be used within the sub query.
Set-Comparison Operators:
The set-comparison operators EXIST, IN, and UNIQUE, along with their negated versions. SQL also
supports op ANY and op ALL, where operator is one of the arithmetic comparison operators {<, <=, =,
<>, >=, >}.
Ex: Find sailors whose rating is better than some sailor called Horatio.
SELECT S.sid FROM Sailors S WHERE S.rating > ANY (SELECT S2.rating FROM Sailors S2
WHERE S2.sname = „Horatio‟);
If there are several sailors called Horatio, this query finds all sailors whose ratingis better than that of
some sailor called Horatio. On instance sailors this computes the sids 31, 32, 58, 71, and 74.
Ex: Find sailors whose rating is better than every sailor' called Horatio.
SQL> SELECT S.sid FROM Sailors S WHERE S.rating > ALL (SELECT S2.rating FROM Sailors S2
WHERE S2.sname = „Horatio‟);
On instance sailors we would get the sid", 58 and 71
AGGREGATE OPERATORS:
To simply retrieving data we often want to perform some computation or summarization. SQL
allows the use of arithmetic expressions. We now consider a powerful class of constructs for computing
aggregate values such as MIN and SUM. These features represent a significant extension of relational
algebra. SQL supports five aggregate operations, which can be applied on any column.
2. SUM ([DISTINCT] A): The sum of all (unique) values in the A column.
Ex: sum the rating of all sailors;
SQL>select sum(rating) from sailors;
3. AVG ([DISTINCT] A): The average of all (unique) values in the A column.
Ex: Average age of all sailors.
SQL> select avg(age) from sailors;
5
Jntuk396.blogspot.com
Jntuk396.blogspot.com
Syntax : SELECT column_name [, aggregate name] FROM <table name> [WHERE <condition>]
GROUP BY column_name;
The „column_name‟ of SELECT command and which used in GROUP BY clause must be same.
The <table name> is name of table.
The aggregate name is any function such as sum(), avg() etc.,
Eg: 1) Find the age of the youngest sailor for each rating level.
SQL> select sname,min(age) from sailors group by rating;
It will display only one sailors name and age for each rating group
Eg: 2) Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years old) for each
rating level with at least h.uo such sailors.
SQL> SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING
COUNT (*) > 1;
HAVING clause : This clause is used in SELECT command and it is similar to WHERE clause, but it
identifies groups which match in a specified condition.
Syntax : SELECT column_name [, aggregate name] FROM <table name> [WHERE <condition>]
GROUP BY column_name HAVING <condition>;
The „column_name‟ of SELECT command and which used in GROUP BY clause must be same.
The <table name> is name of table.
The aggregate name is any function such as sum(), avg() etc.,
The HAVING < condition> contains relational expression and one of the column must be
Aggregate function.
Eg: 1) SELECT JOB FROM EMP GROUP BY JOB HAVING AVG(SAL) > 3000;
It will display only one job name for each group and whose employee average salary is > 3000.
Eg: 2) SELECT AVG(SAL), JOB FROM EMP GROUP BY JOB HAVING MAX(SAL) > 3000;
It will display only one job name, and average salary for each group and whose maximum salary >
3000.
NULL VALUES:
SQL provides a special column value called null to use in such situations. We use null when the column
value is either unknown or inapplicable. Using the Sailor table definition, we might enter the row (98.
Dan, null, 39). The presence of null values complicates many issues these issues are
Comparisons using null values
Logical connectivity AND, OR and NOT
Impact on SQL construct
Outer joins
Disallowing Null Values
6
Jntuk396.blogspot.com
Jntuk396.blogspot.com
Outer Joins:
Interesting variants of the join operation that rely on null values, called outer joins, are supported
in SQL. Consider the join of two tables, say Sailors Reserves. Tuples of Sailors that do not
match some row in Reserves according to the join condition do not appear in the result. In an outer join,
on the other , Sailor rows without a matching Reserves row appear exactly once in the result, with the
result columns inherited from Reserves assigned null values.
In a left outer join,
Sailor rows without a matching Reserves row appear in the result but not vice versa. In a right
outer join, Reserves rows without a matching Sailors row appear in the result, but not vice versa. In a
full outer join, both Sailors and Reserves rows without a match appear in the result.
SQL allows the desired type of join to be specified in the FROM clause. For example, the
following query lists
SQL>SELECT S.sid, R.bid FROM Sailors S LEFT OUTER JOIN Reserves R on s.sid=r.sid;
7
Jntuk396.blogspot.com
Jntuk396.blogspot.com
INTEGER is the underlying, or source, type for the domain ratingval, and every ratingval value
must be of this type. Values in ratingval are further restricted by using a CHECK constraint; in defining
this constraint, we use the keyword VALUE to refer to a value in the domain. By using this facility, we
can constrain the values that belong to a domain using the full power of SQL queries. Once a domain is
defined, the name of the domain can be used to restrict column values in a table.
The optional DEFAULT keyword is used to associate a default value with a domain. If the
domain ratingval is used for a column in some relation and no value is entered for this column in an
inserted tuple, the default value 1 associated with ratingval is used.
A trigger can be thought of as a 'daemon' that monitors a database, and is executed when the
database is modified in a way that matches the event specification. An insert, delete, or update statement
could activate a trigger, regardless of which user or application invoked the activating statement; users
may not even be aware that a trigger was executed as a side effect of their program.
8
Jntuk396.blogspot.com
Jntuk396.blogspot.com
A condition in a trigger can be a true/false statement (e.g., all employee salaries are less than
$100,000) or a query. A query is interpreted as true if the answer set is nonempty and false if the query
has no answers. If the condition part evaluates to true, the action associated with the trigger is executed
for example, we have to examine the age field of the inserted Students record to decide whether to
increment the count.
The keyword new refers to the newly inserted tuple. If an existing tuple were modified, the
keywords old and new could be used to refer to the values before and after the modification.
9
Jntuk396.blogspot.com
Jntuk396.blogspot.com
SQL operators : The SQL operators are used to perform the operation on values of columns. The
SQL operators are four types. They are 1) Arithmetic operators
2) Relational operators 3) Logical operators 4) Special operators.
1) Arithmetic operators : The Arithmetic operators are used to link the two operands for getting a new
results. The Arithmetic operators are Addition(+), Subtraction(-) multiplication (*), division (/), and
modulo (%).
Eg 1 : SELECT emp_no, emp_name, sal+hra FROM emp;
It will display emp number, employee name and sal with adding hra from emp table.
Eg 2 : SELECT emp_no, emp_name, sal - tax FROM emp;
It will display emp number, employee name and sal by subtracting tax from emp table.
Eg 3 : SELECT emp_no, emp_name, sal * 12 / 100 FROM emp;
2) Relational operators : The Relational operators are used to compare two operands for further
process. The Relational operators are
Operator Meaning
> greater than
< less than
>= greater than or equal to
<= less than or equal to
= equal to
<> not equal to
3) Logical operators : The Logical operators are used to compare two relational expressions. The
Logical operators are AND, OR, and NOT operators.
Eg 1 : SELECT *FROM emp WHERE emp_no >= 10 AND emp_no <= 20;
It will display rows of data which emp_no are in between 10 and 20.
Eg 2 : SELECT *FROM emp WHERE emp_no >=10 or sal = 4500;
It will display rows of data which emp_no is greater than or equal to 10 or which sal is equal
to 4500.
4) Special operators : The special operators are used to match the values in given list.
The special operators are
a) BETWEEN operator b) IN/NOT IN operator
c) LIKE operator d) IS NULL operator
a)BETWEEN operator : This operator is used to match the particular column values in the given
range. The range consists lowest value and highest value.
Syntax : BETWEEN low value AND high value ;
Eg: 1 SELECT *FROM emp WHERE emp_no BETWEEN 5 AND 10;
It will display the rows of data from emp table which employee number in between 5 and 10.
Eg: 2 SELECT *FROM emp WHERE sal BETWEEN 5000 AND 10000;
10
Jntuk396.blogspot.com
Jntuk396.blogspot.com
It will display the rows of data from emp table which employee salary is in between 5000 and
10000.
b) i) IN operator: The IN operator is used in where clause to match the values of column in the
given list.
Syntax: IN (list)
b) ii) NOT IN operator: The NOT IN operator is used in where clause to which column values are
not match in the given list.
Syntax: NOT IN (list)
c) LIKE operator: The LIKE operator is used in where clause to match the possible values in the
given list. I.e. it is possible to select rows that match a character pattern. The character pattern
matching operation may be referred to as „wild-card‟ search. The symbols can be used to construct
the search string.
d) IS NULL operator: The IS NULL operator is used in where clause to test the NULL value in the
given column name.
Eg: SELECT * FROM emp WHERE hra IS NULL;
It will display the rows of data from emp table which hra value is null value.
FUNCTION : A function is a process that will manipulates data values and returns the results. It is
similar to an operator. But it should contain the argument. The argument has a constant value.
Rules: 1. If you call a function with an argument, ORACLE converts the argument into
the expected type. For example, Floor(15.7) returns 15. Here, input is float
type and output is integer type.
2. If you call a function with a null argument, the function automatically returns
null value Eg: ABS(-5), its absolute value is 5.
11
Jntuk396.blogspot.com
Jntuk396.blogspot.com
Types of functions : The SQL functions are divided into two types based on the function process.
They are I). Single row functions II). Group functions.
I. Single row functions : A single row functions perform the process based on the input value and
return the output value. In this case, it takes input one type of constant and produces another type. For
example, Floor(15.7) returns 15. Here, input is float type and output is integer. The single row
functions must use the keyword dual. The single row functions are
i) Number functions ii) Character functions iii) Date functions
iv) Conversion functions v) Other functions.
I)Number functions : Number functions accept numeric value as input and return numeric value as
output. Most of these functions return values that are accurate to 38 decimal digits. The functions are
2) CEIL (Ceiling) : This function requires float value and returns the integer value. Suppose, the value
after decimal point is greater than 0 then it will add one to integer part.
Syntax : CEIL( n) Where n is numeric value.
Eg: SELECT CEIL(15.7) FROM DUAL; Eg: SELECT CEIL(15.4) FROM DUAL;
It will display the output value as 16. It will display the output value as 16.
6) FLOOR() : This function is used to returns the integer i.e. it will drop the decimal part .
Syntax : FLOOR( n) Where n is numeric value.
12
Jntuk396.blogspot.com
Jntuk396.blogspot.com
7) LN(Logarithm to base E) : This function is used to returns the natural logarithm of n. where n is
greater than 0.
Syntax : LN( n) Where n is numeric value.
Eg: SELECT LN(95) FROM DUAL; It will display the natural log of 95 as 4.5538769
8) LOG(Logarithm to base 10): This function is used to returns the logarithm, base m of n. The base
„m‟ can be any positive number other than 0 or 1 and „n‟ can be any positive number.
Syntax:LOG(m,n) Where „m‟ can be positive number and „n‟ can be any positive number.
Eg: SELECT LON(10,100) FROM DUAL; It will display the log base 10 of 100 as 2.
Eg: SELECT MOD(45,12) FROM DUAL; It will display the remainder value of m/n as 9.
Eg: SELECT POWER(3,2) FROM DUAL; It will display the 3 to the power of 2 is as 9.
12) ROUND(): This function is used to return the n places with rounding from the value m. The m must
be the decimal point. If n is omitted, it return the 0 places.
The n must be an integer.
Eg: SELECT ROUND(15.193,1) FROM DUAL; It will display the output as 15.2.
Eg: SELECT ROUND(15.193,2) FROM DUAL;
It will display the output as 15.19.
Eg: SELECT ROUND(15.193,3) FROM DUAL;
It will display the output as 15.193.
Eg: SELECT ROUND(15.193,-1) FROM DUAL;
It will display the output as 20.
13) SIGN(): This function is used to returns -1 if n < 0, returns 0 if n = 0, (or)returns 1 if n > 0.
Syntax: SIGN( n ) Where „n‟ is numeric value.
13
Jntuk396.blogspot.com
Jntuk396.blogspot.com
16 SQRT(): This function is used to returns the square root of n. The n value must not be negative.
Sqrt returns a “real” result.
19 TRUNC(): This function is used to truncate the decimal values and display specified number of
decimal places from the given number.
II) CHARACTER FUNCTIONS: This function is used accept single row character as input and can
return both character and number values. The character type value (or) column may be defined with the
keyword char or varchar2. The character functions are
1) CHR () : This function is used to returns the character value. I.e. ASCII character.
14
Jntuk396.blogspot.com
Jntuk396.blogspot.com
Syntax: CONCAT(char1,char2) The char1 and char2 are character data or character variables.
3) INITCAP() : This function is used to display the first letter of each word in uppercase, all other
letters in lowercase. It is allowed in alphabetic characters only.
5) LPAD() : This function is used to display the specified symbol if „n‟ is more than the given character
data.
6) LTRIM() : This function is used to remove the characters from the left of character data. That is, if
the specified right most characters are same of first two characters in the character data.
Syntax: LTRIM(char) The char is alphanumeric type data.
15
Jntuk396.blogspot.com
Jntuk396.blogspot.com
7) NLS_INITCAP() : This function is used to display the first character in upper case for all words
which in the character data. It is similar to INITCAP() function.
8) NLS_LOWER() : This function is used to display all characters in lower case. It is same as the
LOWER() function.
9) NLS_UPPER() : This function is used to display all characters in upper case. It is same as the
UPPER() function.
10) REPLACE function() : This function is used to replace the letter or word with given letter or word.
11) RPAD function() : This function is used to replicate the given character a specified number of time.
That is, if the given number is grater than the stirng(character data) it will print the given character.
16
Jntuk396.blogspot.com
Jntuk396.blogspot.com
12) RTRIM() : This function is used to remove the characters from the right of character data. That is,
if the specified characters are same of last characters in the character data then it removes those
characters.
Syntax: RTRIM(char) The char is alphanumeric type data.
13) SUBSTR() : This function is used to display the character from specified position to the specified
number characters in the given character data.
Syntax: SUBSTR(char,pos, n)
The char is alphanumeric type data.
The pos is position in the given data.
The n is number that specifies number characters from the position.
14) UPPER() : This function is used to display all characters in upper case. It is same as the
NLS_UPPER() function.
15) ASCII() : This function is used to convert the given character into equivalent ascii code.
Syntax: ASCII(char) The char must be alphabetic type data only.
16) INSTR() : This function is used to display the specified position character at what location in the
given character data.
Syntax: INSTR(char,pos, n)
The char is any alphabetic type data.
The pos is used to specify that the character is at what position.
The „n‟ specifies how many times the character is in the data.
Eg: SELECT INSTR(„we are all ssn studnets‟,‟s‟,12,2) FROM DUAL;
It will display the second letter „s‟ at what position in data. That is at 13.
17
Jntuk396.blogspot.com
Jntuk396.blogspot.com
17) LENGTH() : This function is used to display the length of given character data (string). It count the
null space also.
Syntax: LENGTH(char)
The char is any alphanumeric type data.
Eg: SELECT LENGTH(„ssn college‟) FROM DUAL;
It will display the length of character data (string). I.e. 11
III)DATE functions : Date functions operate on values of DATE data type. All date functions return
a value of DATE data type, except the MONTHS_BETWEEN function, which returns a number.
1) ADD_MONTHS() : This function is used to return the DAY plus n months. It means, return a date
by adding specified number of months. For example, the give date is „17-dec-08‟, it will be display
as „17-jan-81‟ when we added „one‟ to it.
Syntax : ADD_MONTHS(d,n)
The d may be date (or) date variable.
The n is the integer number, it is used to add number of months to the month. For
example
Eg : select hiredate from emp;
It will display 14 rows of column hiredate values.
Eg: select add_months(hiredate,1) from emp;
It will display 14 rows of column hiredate values from emp with adding one month
Eg: select add_months(hiredate,3) from emp;
It will display 14 rows of column hiredate values from emp with adding 3 months
2)LAST_DAY(): This function returns the date corresponding to the last day of the months. Suppose,
the system date is 17-sep-08. The Last day of this month is 31-sep-08.
Syntax : Last_day(Column_name); The column_name must be date type.
3)MONTHS_BETWEEN(): This function returns a number of months between dates i.e date1 and
date2. If date 1 is later than date2,then result is negative. If the date1 is earlier than date2, result is
negative.
Syntax: MONTHS_BETWEEN(date1,date2)
The date1 and date2 are date type.
For example, 1)select months_between(„17-sp-08‟,‟1-oct-08‟) from dual;
2)select months_between(hiredate,sysdate) from emp;
4)NEXT_DAY(): This function is used to return the date of the first week day.
It means later than the date „d‟.
Syntax: NEXT_DAY(d,char)
The „d‟ is date value (or) date type identifier (or) sysdate keyword.
The char is any week day such as „Monday‟,‟Tuesday‟, . . . . .
This day must later day of „d‟
Eg: Select next_day(„17-sep-08‟,‟Thursday‟) from dual;
18
Jntuk396.blogspot.com
Jntuk396.blogspot.com
In this „17-sep-08‟ is today date and its day is „Wednesday‟ . But, it will display the next day of
Thursday. That is „Friday‟ . So, it will display Friday date as „19-sep-08‟.
5)NEW_TIME(): This function is used to return the date and time.
Syntax: NEXT_TIME(d,z1,z2)
The „d‟ is date value (or) date type identifier (or) sysdate keyword.
The z1 and z2 are time zones
The z2 is used to display the date of z1 format in z2 format.
i.e if z1 is AST format ad z2 is CST format, then the date of z1 format will be displayed in z2 format.
6) ROUND(): This function returns the date which rounded to the unit specified by the format „fmt‟ . It
means, it round to the nearest day.
Syntax: ROUND(d, fmt)
The letter „d‟ represents date.
The word „fmt‟ represents year, month (or) day
Eg: 1)SELECT hiredate, ROUND(hiredate,‟year‟) from emp;
Eg: 2)SELECT hiredate, ROUND(hiredate,‟month‟) from emp;
Eg: 3)SELECT hiredate, ROUND(hiredate,‟day‟) from emp;
Eg: 4)SELECT ROUND(sysdate) from dual;
Eg: 5)SELECT ROUND(sysdate,‟year‟) from dual;
Eg: 6)SELECT ROUND(sysdate,‟month‟) from dual;
Eg: 7)SELECT ROUND(sysdate,‟day‟) from dual;
7)SYSDATE: This keyword is used to return the current date and time.
Syntax : SYSDATE;
Eg: SELECT SYSDATE FROM DUAL;
Eg: SELECT SYSDATE „mm-dd-yyyy hh24‟ NOW FROM DUAL;
8) TRUNC() : This function is used to returns date with the time portion of the day truncated which is
specified by format „fmt‟. If „fmt‟ is omitted, the date is converted to the nearest day.
For example, if the SYSDATE is „27-jan-99‟, then truncated result is 01-jan-99. It means, 27 truncated
as 01 in the date.
Syntax: TRUNC(d, fmt)
Eg: 1)SELECT TRUNC(sysdate,‟year‟) from dual;
Eg: 2)SELECT TRUNC(sysdate,‟month‟) from dual;
Eg: 3)SELECT TRUNC(sysdate,‟day‟) from dual;
Eg: 4)SELECT TRUNC(hiredate,‟year‟) from emp;
Eg: 5)SELECT TRUNC(hiredate,‟month‟) from emp;
Eg: 6)SELECT TRUNC(hiredate,‟day‟) from emp;
Eg: 7)SELECT TRUNC(sysdate) from dual;
19
Jntuk396.blogspot.com
Jntuk396.blogspot.com
IV : CONVERSION FUNCTIONS :The conversion functions are used to convert a value from one
„data type‟ to another. It means, char type to number type and so on. The Conversion functions are
1) TO_CHAR(): The function is used to convert date into varchar2 type in the default date format.
Syntax : TO_CHAR(date,[fmt]);
2) TO_NUMBER():This function allows the conversion of string (varchar2) type data (number) into
number type in the specified format.
Syntax: TO_NUMBER(char[,fmt]);
The word „char‟ is character type data (or) variable
The word „fmt‟ is optional that represent format (i.e. AST/BST/CST etc.,
Eg: SELECT TO_NUMBER(„100‟) FROM DUAL;
3) TO_DATE() :This function is used to converts character type data into date type data.
Syntax: TO_DATE(char[,fmt]);
The word „char‟ is character type data (or) variable
The word „fmt‟ is optional that represent format (i.e. AST/BST/CST etc.,
Eg: SELECT („September 18 2008‟ , „ month-dd-yyyy‟) from dual;
4) ROWIDTOCHAR() :This function is used to convert ROWID value into character type value(I.e
varchar2 type). Syntax: ROWIDTOCHAR(rowid);
The word „rowid‟ is a keyword that generate numbers automatically at the time of adding rows into
table.
Eg: SELECT ROWIDTOCHAR(rowid) from dual/emp;
V) OTHER FUNCTIONS : The other functions are GREATEST(), LEAST(), NVL() and VSIZE().
1) GREATEST(): This function is used to find greatest string from given list of strings.
Syntax : GREATEST(string1,string2,string3, . . . . .);
Eg: SELECT GREATEST(‟BVSR‟, „SSN‟,‟RISE‟,‟PASE‟,‟QIS‟) FROM DUAL;
2) LEAST(): This function is used to find least string from given list of strings.
Syntax : LEAST(string1,string2,string3, . . . . .);
Eg: SELECT LEAST(‟BVSR‟, „SSN‟,‟RISE‟,‟PASE‟,‟QIS‟) FROM DUAL;
3) NVL(): This function is used to display the specified message which column contain „null‟ values in
a row. Syntax : NVL(exp1,exp2, . . . . . . . .);
The word „exp1‟ is any column name of specified table
The word „exp2‟ is any string that used to replace by null value.
4) VSIZE() : This function is used to find the number of bytes the value occupies in the given variable
or column name.
Syntax: VSIZE(column_name);
Eg: SELECT ENAME VSIZE(ENAME) „SIZE IN BYTES‟ FROM EMP;
20
Jntuk396.blogspot.com