Skip to main content

Importance of SQL Character Functions in Oracle Database #DOTC_MDB

 Importance of SQL  Character Functions  in Oracle Database

 

Hello Everyone,

Today we will discuss about SQL functions in Oracle which plays important role in writing SQL queries. Before Starting to read this blog I suggest you to please go through my previous basic SQL blog for your reference.

We will discuss the below Functions in today’s blog.

1. ROUND

2. TRUNC

3. CEIL and FLOOR

4. SIGN AND ABS

5. NVL and NVL2

 

Let’s discuss Round Function in SQL: ROUND function is used to round a numeric value to a specified precision. If the value is less than 5, the function rounds down. If the value is greater than or equal to 5, the function rounds the value up. The function contains two parameters: The first is the value or column name, and the second is the precision. Round Function also used to suppress decimal values. ROUND Function enables you to round a numeric value that is returned in a result set.

Example of ROUND function to round one number up and another number down and also using ROUND function to round numeric values to whole numbers is as below. I use UNION Operator to see the output of both the query in one for better understanding.

SELECT ROUND(987654.1238, 2), ROUND(987654.8591, 2)

  FROM DUAL

UNION

SELECT ROUND(987654.1238),  ROUND(987654.8591)

  FROM DUAL ;

Output of above query is as below:



 

Another example of ROUND function to round numeric values to the left of the decimal point is as below.

select ROUND(987654.1238, -1), ROUND(987654.8591, -3)

  FROM DUAL;

Output of above query is as below

 


2. TRUNC : TRUNC function is an inbuilt function in PLSQL which is used to return a number truncated to a particular number of decimal places.

NOTE : Difference between ROUND and TRUNC is The ROUND function is used to round a numeric field to the nearest number of decimals specified whereas TRUNC Function returns a number truncated to a certain number of decimal places. So it always takes the lesser value.

Example : For Round 76 is nearer to 80 than 70 so ROUND  will return 80 whereas TRUNC function will return 70 even we use 74 also.

Let’s take an example of using TRUNC function to cut off digits to the right of the decimal point and to truncate numeric values to the whole numbers. I am using union operator to see the output of both.

SELECT TRUNC(987654.1238, 2), TRUNC(987654.8591, 2)

  FROM DUAL

UNION

SELECT TRUNC(987654.1238),  TRUNC(987654.8591)

  FROM DUAL ;

Output of above query is as below

 


Another Example of TRUNC function to truncate numeric values to the left of the decimal point, below is the query for that.

 

SELECT TRUNC(987654.1238, -1), TRUNC(987654.8591, -3)

  FROM DUAL;

Output of above query is as below.

 


If you see the output of both ROUND and TRUNC you will be able to understand the difference between both of them.

 

3. CEIL AND FLOOR:  CEIL function in Oracle returns the smallest integer value that is bigger than or equal to a number whereas the FLOOR function returns the largest integer value not greater than a number specified as an argument.

 

Below is the query for both CEIL and FLOOR . I used UNION operator to see the output of both function.

SELECT CEIL(987654.1238),  CEIL(987654.8591)

  FROM DUAL

UNION

SELECT FLOOR(987654.1238),  FLOOR(987654.8591)

  FROM DUAL ;

 

Output of above query is as below the second value in output is for FLOOR function.

 


Using ROUND, CEIL, TRUNC, and FLOOR functions in one query to see the output. Below is the Query.

SELECT ROUND(987654.8591), CEIL(987654.8591), TRUNC(987654.8591), FLOOR(987654.8591) FROM DUAL;

Output of above query is as below.

 


4. SIGN and ABS Function: ABS function in SQL returns the absolute value of a specified value. You provide the value as an argument. The return value is of the same type as the argument whereas SIGN Function returns the sign of a number , if number is 0 it will return 0 if number is less than 0 it returns  -1 and if number is greater than 0 it returns 1.

Below is the query to understand ABS function. I used UNION ALL operator to see the output of both positive and negative number as ABS function in SQL returns the absolute value of a specified value.

SELECT ABS(567) FROM DUAL

UNION ALL

SELECT ABS(-567) FROM DUAL;

Output of above query is as below.

 


Below is the example of SIGN with positive, negative and zero value.

SELECT SIGN(567),SIGN(-567),SIGN(0) FROM DUAL;

Output of above query is as below.

 


Using both SIGN and ABS in one query to see the data.

SELECT SIGN(ABS(567) *25 +25)

FROM DUAL

UNION ALL

SELECT SIGN(ABS(-567) *25 +25)

FROM DUAL

UNION ALL

SELECT SIGN(ABS(0) *25 +25)

FROM DUAL;

Output of above query is as below:

 


5. NVL and NVL2 : Oracle NVL function allows you to suppress NULL values with any value passed by expression 1 whereas The NVL2 function examines the first expression. If the first expression is not null, then the NVL2 function returns the second expression. If the first expression is null, then the third expression is returned.

Syntax of NVL :

NVL (EXPR1, EXPR2)

EXPR1 : Expression that may contain a null whereas EXPR2 suppress the NULL values with your argument.

Syntax of NVL2 :

NVL2 (EXPR1, EXPR2, EXPR3)

Taking one example from employee table where commission field is null only value for commission is 2 records i.e. 12 . Below is the query

SELECT  LOCATION,SALARY, NVL(COMMISION, 0),

    (SALARY*2) + (SALARY*2*NVL(COMMISION, 0))

      COMMISION_VALUE FROM EMPLOYEE;

Output of above query is as below. Note :Data will not updated in base table.

 


Example of NVL2 function is as below , in the below query if first expression is not null then it will give the value as salary + commission and for null value it will return Salary .

SELECT LOCATION, SALARY, COMMISION,

 NVL2(COMMISION, 'SALARY+COMMISION', 'SALARY')

 TOTAL_VALUE FROM EMPLOYEE;

Output of above query is as below:

 


Please read this blog carefully and let me know if you have any question as this functions are very important and vastly used in SQL and PL/SQL block . In this blog we discussed Oracle number and miscellaneous substitution functions. In next blog I will come up with more SQL functions.

Thanks.

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you que...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...