Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Thursday, April 17, 2014

Simple Example with REPLACE

The REPLACE function allows you to change a string of characters to another string of characters and can accept three parameters:
 
(1)    Input column name.
(2)    Old string value.
(3)    New string value.
 
You can see what I mean in the example below, which I tested on Oracle 11.2:
 
SQL> create table directory_name
  2  (location varchar2(30))
  3  /
 
Table created.
 
SQL> insert into directory_name
  2  values('/batch/prod/dir1')
  3  /
 
1 row created.
 
SQL> insert into directory_name
  2  values('/batch/prod/dir2')
  3  /
 
1 row created.
 
SQL> select location from directory_name
  2  /
 
LOCATION
------------------------------
/batch/prod/dir1
/batch/prod/dir2
 
SQL> update directory_name
  2  set location = replace(location,'prod','test')
  3  /
 
2 rows updated.
 
SQL> select location from directory_name
  2  /
 
LOCATION
------------------------------
/batch/test/dir1
/batch/test/dir2
 
SQL>

Friday, January 24, 2014

Hard Coded Variables in PL/SQL

Many applications need to use the same value in several different places. The examples below show a net value and a tax figure being added to create a gross amount. The tax figure is calculated by multiplying the net value by the tax rate. In the first example, the tax rate is hard-coded as 0.2. This is quite simple to implement but, if the tax rate changes, you will need to go through your whole application and amend every individual value: 

SQL> set serveroutput on
SQL> declare
  2    net number;
  3    tax number;
  4    gross number;
  5  begin
  6    net := 1;
  7    tax := 0.2;
  8    gross := net + (net * tax);
  9    dbms_output.put_line('Gross = '||to_char(gross,'$9.99'));
 10  end;
 11  /
Gross =  $1.20
 
PL/SQL procedure successfully completed.

SQL> 

The second example stores the tax rate as a constant in a package. When you need to know the tax rate, you fetch it from the package as shown below. This is slightly harder to implement but, if the tax rate changes, you only need to amend the value in one place: 

SQL> create or replace package special_values1
  2  is
  3    tax constant number := 0.2;
  4  end special_values1;
  5  /
 
Package created.
 
SQL> declare
  2    net number;
  3    tax number;
  4    gross number;
  5  begin
  6    net := 1;
  7    tax := special_values1.tax;
  8    gross := net + (net * tax);
  9    dbms_output.put_line('Gross = '||to_char(gross,'$9.99'));
 10  end;
 11  /
Gross =  $1.20
 
PL/SQL procedure successfully completed.

SQL>

The third example is similar to the second but, this time, the tax rate is returned by a function:
 
SQL> create or replace package special_values2
  2  is
  3    function tax
  4    return number;
  5  end special_values2;
  6  /
 
Package created.
 
SQL> create or replace package body special_values2
  2  is
  3    function tax
  4    return number
  5  is
  6  begin
  7    return 0.2;
  8  end;
  9  end special_values2;
 10  /
 
Package body created.
 
SQL> declare
  2    net number;
  3    tax number;
  4    gross number;
  5  begin
  6    net := 1;
  7    tax := special_values2.tax();
  8    gross := net + (net * tax);
  9    dbms_output.put_line('Gross = '||to_char(gross,'$9.99'));
 10  end;
 11  /
Gross =  $1.20
 
PL/SQL procedure successfully completed.
 
SQL>

The simplest method of all is possibly to store the tax rate as a value in a table. I won't insult your intelligence by showing an example using this method!

Saturday, September 15, 2012

MOD Function

This was tested on Oracle 11.1.0.6.0 running on Windows XP. The mod function calculates the remainder when you divide the first value by the second:

SQL> select mod(10,7) from dual;

 MOD(10,7)
----------
         3

SQL> select mod(-11,6) from dual;

MOD(-11,6)
----------
        -5

SQL> select mod(9,-5) from dual;

 MOD(9,-5)
----------
         4

SQL> select mod(-8,-3) from dual;

MOD(-8,-3)
----------
        -2

SQL>

The numbers do not have to be integers:

SQL> select mod(4,1.5) from dual;

MOD(4,1.5)
----------
         1

SQL> select mod(6.2,3) from dual;

MOD(6.2,3)
----------
        .2

SQL>

... but I was a bit surprised that the next calculation did not return a division by zero error:

SQL> select mod(2,0) from dual;

  MOD(2,0)
----------
         2

SQL>

ABS Function

This was run on Oracle 11.1.0.6.0 on Windows XP. The ABS function returns the absolute value of a column or value:

SQL> select abs(4.3) from dual;

  ABS(4.3)
----------
       4.3

SQL> select abs(-2.9) from dual;

 ABS(-2.9)
----------
       2.9

SQL>

Tuesday, August 30, 2011

DBA_SOURCE


Tested on an Oracle 9 database. Imagine you have a function call which returns a value as follows:

SQL> select sys.login_user from dual;
 
LOGIN_USER
--------------------------------------------------
ORACLE
 
SQL>
  
You can see its source code stored in DBA_SOURCE:

SQL> desc dba_source
Name                    Null?    Type
----------------------- -------- ----------------
OWNER                            VARCHAR2(30)
NAME                             VARCHAR2(30)
TYPE                             VARCHAR2(12)
LINE                             NUMBER
TEXT                             VARCHAR2(4000)
 
SQL> l
  1  select text from dba_source
  2  where owner = 'SYS'
  3  and name = 'LOGIN_USER'
  4* order by line
SQL> /
  
TEXT
--------------------------------------------------
function login_user return varchar2 is
begin
return dbms_standard.login_user;
end;
 
SQL>

The TYPE column shows what type of object the source came from. There are several possibilities:
  
SQL> select distinct type from dba_source;
 
TYPE
------------
FUNCTION
JAVA SOURCE
PACKAGE
PACKAGE BODY
PROCEDURE
TRIGGER
TYPE
TYPE BODY
 
8 rows selected.
 
SQL>
  
In the example above, the source came from a function, which you might have guessed as it returns a value to the user:
  
SQL> l
  1  select distinct type from dba_source
  2  where owner = 'SYS'
  3* and name = 'LOGIN_USER'
SQL> /
 
TYPE
------------
FUNCTION
 
SQL>