Monday 18 March 2013

PLSQL Operators

PL SQL Operators


Relational operators compare two expressions or values and return a Boolean result. PL/SQL supports operators like

=, <, >, <=, >=, <>, !=, ~=, ^=, IS NULL, LIKE, BETWEEN, IN


Operator Meaning

=

equal to

<>, !=, ~=, ^=

not equal to

<

less than

>

greater than

<=

less than or equal to

>=

greater than or equal to

Is NULL operator checks if
Example code:

declare
   a number ( 2 ) := 50 ;
   b number ( 2 ) := 15 ;
begin
   if ( a = b ) then
      dbms_output.put_line ( '1-a is equal to b' );
   else
      dbms_output.put_line ( '1-a is not equal to b' );
   end if;
   if ( a < b ) then
      dbms_output.put_line ( '2-a is less than b' );
   else
      dbms_output.put_line ( '2-a is not less than b' );
   end if ;
   if ( a > b ) then
      dbms_output.put_line ( '3-a is greater than b' );
   else
      dbms_output.put_line ( '3-a is not greater than b' );
   end if ;
   -- Lets change value  a and b
   a      := 40 ;
   b      := 90 ;
   if ( a <= b ) then
      dbms_output.put_line ( '4-a is either equal or less than b' );
   end if ;
   if ( b >= a ) then
      dbms_output.put_line ( '5-b is either equal or greater than a' );
   end if ;
   if ( a <> b ) then
      dbms_output.put_line ( '6-a is not equal to b' );
   else
      dbms_output.put_line ( '6-a is equal to b' );
   end if ;
end;
Output:



IS NULL Operator

The IS NULL operator returns the Boolean value TRUE if its operand is null or FALSE if it is not null. test for the state of being null IF variable IS NULL THEN


LIKE Operator

You use the LIKE operator to compare a character, string, or CLOB value to a pattern. Case is significant. LIKE returns the Boolean value TRUE if the patterns match or FALSE if they do not match.

The patterns matched by LIKE can include two special-purpose characters underscore _ matches one character and percent sign % matches zero or more characters
example:
select var_name from table_names where var_name LIKE 'JOHN%';

you will get all names starting with JOHN like JOHN, JOHNSON,JOHNATHON etc...

BETWEEN Operator

The BETWEEN operator tests whether a value lies in a specified range. It means "greater than or equal to low value and less than or equal to high value."
Example:
If you want to select some record between some input date and system date

Select * from SOME_TABLE where to_char(beginning_date,'yyyymmdd') between to_char(i_first_date,'yyyymmdd') and to_char(sysdate,'yyyymmdd')

IN Operator

The IN operator tests set membership. It means "equal to any member of
 select * from table names where var_name IN ('JOHN','ERIC');
You will get all records containing names of JOHN and ERIC...













Tuesday 12 March 2013

PLSQL Modulo 11 check...

What is Modulo 11 check?

MSI was developed by the MSI Data Corporation, based on the original Plessey Code. MSI, also known as Modified Plessey, is used primarily to mark retail shelves for inventory control. MSI is a continuous, non-self-checking symbology. While the length of an MSI bar code can be of any length, a given application usually implements a fixed-length code. 
A typical MSI bar code is


MSI, and other symbologies based on Pulse-Width Modulation, offer no significant benefit over more modern symbologies. While it is not a bad idea to support MSI for legacy bar codes, most new applications do not choose MSI as their symbology of choice.


COMPUTING THE CHECKSUM DIGIT MSI 

uses one or two check digits, which may be calculated in a number of different ways. As such, it is really up to the software application to implement and check the check digit(s).

The most common methods of calculating MSI check digits are: Modulo 10 and Modulo 11


Modulo 11 is what we want which is developed by IBM company based on Lhun algorithm...

A check digit is a number that is used to validate a series of numbers whose accuracy you want to insure. Frequently the last digit of a number string such as identification number is a check digit. Lets say the identification number starts out at 6 digits. A calculation is done using the six digits and a seventh digit is produced as a result of the calculation. This number is the check digit. There are many calculations that can be used - this example illustrates the logic of the MOD11 check digit.
Steps to calculate the MOD11 check digit for a number such as an id #:

    • Assign weights to each digit of the id #. The weights in MOD11 are from 2 through a maximum of 10 beginning with the low order position in the field.
    • Each digit in the id # is multiplied by its weight
    • The results of the multiplication are added together
    • This product is divided by the modulus number 11
    • The remainder is subtracted from the modulus number 11 giving the check digit
Example: find the check digit for the number 036532

0
3
6
5
3
2
x7
x6
x5
x4
x3
x2
0
18
30
20
9
4
0 + 18 + 30 + 20 + 9 + 4 = 81
81/11 = 7 remainder 4
11 - 4 = 7
7 is therefore the check digit.
PROBLEMS: If the remainder from the division is 0 or 1, then the subtraction will yield a two digit number of either 10 or 11. This won't work, so if the check digit is 10, then X is frequently used as the check digit and if the check digit is 11 then 0 is used as the check digit. If X is used, then the field for the check digit has to be defined as character (PIC X) or there will be a numeric problem.
Steps to verify if the check digit is included as part of the number:

    • The entire number is multiplied by the same weights that were used to calculate and the check digit itself is multiplied by 1.
    • The results of the multiplication are added together.
    • The sum is divided by 11 and if the remainder is 0, the number is correct.
PROBLEM: Note that if the check digit is X then 10 is used in the multiplication. Code for this occurrence must be included.
Example of verifying the number 0365327 where 7 is the calculated MOD11 check digit:
0
3
6
5
3
2
7
x7
x6
x5
x4
x3
x2
x1
0
18
30
20
9
4
7
0 + 18 + 30 + 20 + 9 + 4 + 7 = 88
88/11 is 8 remainder 0
Since the remainder from this calculation is 0, the check digit 7 is valid.

So without any other complications here is the PL code...

procedure modulo_11 (l_input in varchar2) as
l_cntrl_num_sum number(1):= 0;
l_pom varchar2(30):=' ';
l_length number(30):= 0;
l_length_pom number(30):= 0;
l_sum number(30):= 0;
l_error number(2) := 0;
----
l_counter number(30);
l_sum1 number(30):=0;
l_sum_uk number(30):=0;
l_reminder number(30) := 0;
l_control_number number(30) := 0;
l_counter1 number(2) := 0;
l_errmsg varchar2(200) := ' ';
e_exit exception;
begin
-- test input
--l_input  := '102305789016';
   -- determine length of input num
   l_length := length(l_input);
   -- take last digit for control
   l_cntrl_num_sum := substr(l_input,l_length,1);
   -- set length -1 sign
   -- take length in some local variable
   l_length_pom := l_length;
   -- set variable counters to zero
   l_counter := 0;
   l_reminder := 0;
   l_control_number := 0;
   l_counter :=0;
   -- counter to 1
   l_counter1 := 1;
   l_pom := null;
   -- reversing string input
   select reverse(l_input) into l_pom from dual;
   -- taking second place till last in local var which is used for calculating
   l_pom := substr(l_pom,2,l_length_pom - 1);
   l_length := length(l_pom);
   if l_length is null then
      l_error := 2;
      raise e_exit;
   end if;
   -- starting algorithm
   for k in 1..l_length
      loop
         l_sum := 0;
         l_sum1 :=0;
         l_counter := l_counter + 1;
         l_counter1 :=  l_counter1 + 1;
         l_sum := l_counter1 * substr(l_pom,l_counter,1);
         l_sum1 := l_sum;        
         l_sum_uk := l_sum_uk + l_sum1;
         l_length_pom := l_length_pom - 1;
      end loop;
   l_length := length(l_input) - 1;
   l_reminder := mod(l_sum_uk,11);
   l_control_number := 11 - l_reminder;
   if l_reminder in (0, 1) then
      l_control_number := 0;
   end if;
   if l_control_number != l_cntrl_num_sum then
      l_error := 2;
      l_errmsg := ('error in MOD_11_INI :');
      raise e_exit;
   end if;
exception
   when e_exit then
      raise_application_error(-20010,l_errmsg||' '||sqlcode);
  END modulo_11;

enjoy.....
text sources: 
http://www.pgrocer.net/Cis51/mod11.html
http://en.wikipedia.org/wiki/Luhn_algorithm
http://en.wikipedia.org/wiki/MSI_Barcode

Monday 11 March 2013

PLSQL backup time...

PL SQL package backup tutorial

I started this project because i needed program that will easily back up my files from developer without clicking on package and exporting it to save my work...
Second i wrote it because there is Bug in SQL DEVELOPER that causes lines to be messed up  when you have large code...
Example:

You have large code and export it with right click on package name and select save specs and body

Real code is written like this...

1 line - BEGIN
2 LINE - SELECT RTRIM(SUBSTR(INSTANCE_NAME,1,8))||'-'||RTRIM(SUBSTR(HOST_NAME,1,15))
3 line - INTO L_INPUTNAME
4 line - FROM V$INSTANCE ;
5 line - EXCEPTION WHEN OTHERS THEN
6 LINE - NULL;
7 line - END;

but sometimes export does the weird thing and saves it messed up
switching the line order....
1 line - BEGIN
2 line - SELECT RTRIM(SUBSTR(INSTANCE_NAME,1,8))||'-'||RTRIM(SUBSTR(HOST_NAME,1,15))
4 line - FROM V$INSTANCE ;
3 line - INTO L_INPUTNAME
5 line - EXCEPTION WHEN OTHERS THEN
6 line - NULL;
7 line - END;

I googled this program somewhere on the web two years ago and manage to rewrite it for my needs...
So without any chit chat let's see the code

CREATE OR REPLACE
PROCEDURE BACKUP_OF_PL_FILES
    (I_TIPE_OF_OBJECT    VARCHAR2
    ,I_NAME_OF_OBJECT    VARCHAR2 DEFAULT '%')
IS
    PL_OUTPUT    UTL_FILE.FILE_TYPE;
    L_READ UTL_FILE.FILE_TYPE;
    L_BUFFER    VARCHAR2(4000);
    G_SESSONID    VARCHAR2(80);
    L_LINE        VARCHAR2(80);
    L_COUNTER        NUMBER(30) :=1 ;
    L_SAVE VARCHAR2(32767);
    L_INPUTNAME    VARCHAR2(20);
    L_ERRNUM    NUMBER := 0;
    L_DIRNAME VARCHAR2(100) := ' ';
BEGIN
  
   G_SESSONID := USERENV('sessionid');
   L_DIRNAME := 'BACKUP_PL/'||I_NAME_OF_OBJECT||'/'||TO_CHAR(TO_DATE(SYSDATE,'dd.mm.yy'),'yyyymmdd')||'/';
   -----------------------------------------------------------------------------
  
    BEGIN
        SELECT    RTRIM(SUBSTR(INSTANCE_NAME,1,8))||'-'||RTRIM(SUBSTR(HOST_NAME,1,15))
        INTO    L_INPUTNAME
        FROM    V$INSTANCE ;
    EXCEPTION WHEN OTHERS THEN
        NULL;
    END;

    IF I_NAME_OF_OBJECT='%' THEN
        G_SESSONID    := 'BAK_'||INITCAP(I_TIPE_OF_OBJECT)||'_'||L_INPUTNAME||'_'||TO_CHAR(SYSDATE, 'DDMMYY_HH24MI') || '.pls';
    ELSE
        G_SESSONID    := 'BAK_'||INITCAP(I_NAME_OF_OBJECT)||'_'||L_INPUTNAME||'_'||TO_CHAR(SYSDATE, 'DDMMYY_HH24MI') || '.pls';
    END IF;   
  
   PL_OUTPUT := UTL_FILE.FOPEN('c:\BACKUP', G_SESSONID,'A',4000);      
   UTL_FILE.PUT_LINE(PL_OUTPUT, 'c:\BACKUP' || L_DIRNAME || G_SESSONID);

    FOR RS_TYPE IN (SELECT    DISTINCT TYPE, NAME
            FROM    USER_SOURCE
            WHERE    TYPE LIKE UPPER(I_TIPE_OF_OBJECT)||'%'
            AND    NAME LIKE UPPER(I_NAME_OF_OBJECT)
            ORDER BY 2,1 )
    LOOP
        FOR RS_NAME IN (SELECT    TEXT
                FROM    USER_SOURCE
                WHERE    TYPE = RS_TYPE.TYPE
                AND    NAME = RS_TYPE.NAME )
        LOOP
            BEGIN
                IF (L_COUNTER = 1 OR UPPER(RS_NAME.TEXT) LIKE '%PACKAGE BODY%') THEN
                    L_BUFFER := 'CREATE OR REPLACE '||RS_NAME.TEXT ;
                ELSE
                    L_BUFFER := RTRIM(RS_NAME.TEXT,' ');
                END IF;
                IF SUBSTR(L_BUFFER,-1,1) = ';' THEN
                    UTL_FILE.PUT_LINE(PL_OUTPUT,RTRIM(L_BUFFER));
                ELSE
                    UTL_FILE.PUT_LINE(PL_OUTPUT,RTRIM(L_BUFFER,SUBSTR(L_BUFFER,-1,1)));
                END IF;
                L_COUNTER := L_COUNTER + 1 ;
           
            EXCEPTION WHEN OTHERS THEN
                L_ERRNUM :=1 ;
                DBMS_OUTPUT.PUT_LINE('error 2nd loop...'||SQLERRM);
            END ;
        END LOOP;
        L_COUNTER := 1;
        UTL_FILE.PUT_LINE(PL_OUTPUT,'/');              
        IF RS_TYPE.TYPE NOT LIKE '%PACKAGE%' OR RS_TYPE.TYPE LIKE '%PACKAGE BODY%' THEN
          UTL_FILE.PUT_LINE(PL_OUTPUT,L_LINE);   
        END IF;
    END LOOP;
    UTL_FILE.FCLOSE(PL_OUTPUT);
   L_READ := UTL_FILE.FOPEN('PL_OUT', G_SESSONID,'A',4000);
   ----------------------
EXCEPTION WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Error... '||SQLCODE);
    UTL_FILE.FCLOSE(PL_OUTPUT);
END BACKUP_OF_PL_FILES;

 
Basically this procedure is searching database same as export and write package in your local directory c:\BACKUP

Calling is easy. All you have to do is call it from anonymous block with specification(package,procedure,function...) and name of the Package/Procedure... like this

BEGIN
   BACKUP_OF_PL_FILES('package','MY_PACKAGE_NAME_PKG');
   BACKUP_OF_PL_FILES('package','MY_PACKAGE_NAME_PKG');
   BACKUP_OF_PL_FILES('procedure','MY_PROCEDURE_NAME);
   BACKUP_OF_PL_FILES('function','MY_FUNCTION_NAME');
END;





Sunday 10 March 2013

PLSQL - Decoding time

 PL SQL Decode Function

Some of my friends told me to write something about famous decode :)
So here it goes ....

What is decode?

Decode is implemented function that has the functionality of an IF THEN ELSE statement.
But there is one major difference between implementing these two functionality. DECODE can only work in select statement.You need to have in mind that DECODE is supported only in SQL Context so you don't really working with PL code. More flexible than decode is case statement....
The syntax for the decode function is: decode( expression , search , result [, search , result]... [, default] )
expression is the value to compare.

search is the value that is compared against expression.

result is the value returned, if expression is equal to search.

default is optional. If no matches are found, the decode will return default. If default is omitted, then the decode statement will return null (if no matches are found).


So something to decode now....
 let's create some table


CREATE
   TABLE company
   (
      id            NUMBER(32) NOT NULL,
      company_id    NUMBER(8),
      owner_name    VARCHAR2(20 byte),
      street_adress VARCHAR2(20 byte)
   );

insert some data in it

   insert into company values (1,10001,'Nerad Kovacevic','Neradna BB');
   insert into company values (2,10003,'Vedran Rudjic','Shvalerska 22');
   insert into company values (3,10006,'Ljuban Draga','Vlak u snijegu bb');

select * from company;

you will get 








let's say 10001 is IBM company's id, 10003 is Microsoft and 10006 is TrainStation Co :)

and now we want to display some writings instead of company id's so we decode it
Pseudo-code; if i found id by the number of 10001 i will write 'IBM' if that number is 10003 i will write 'Microsoft' if that number is 10006 i will write 'TrainStation Co' if i don't find (ELSE) number that i have in my decode function i will write 'Nameless'
select decode(company_id,10001,'IBM',10003,'Microsoft',10006,'Trainstation Co','Nameless') as company_name , owner_name, street_adress from company;

after running statement we get;








Lets insert one more row...


insert into company values (3,10009,'Unknown','UNKNOWN bb'); 

and run our statement again and for every undefined id we will get Nameless in company name column...

select decode(company_id,10001,'IBM',10003,'Microsoft',10006,'Trainstation Co','Nameless') as company_name , owner_name, street_adress from company;








So that's about it...

Simple decode function....

Anyway you can find it useful in many occasions and it's use is quite simple and in many situations very grateful...

so one last example

 select decode((select id from company where id = 1 ),1,'IBM',2,'Microsoft',3,'Trainstation Co','Nameless') as company_name , owner_name, street_adress from company where rownum < 2;






Happy decoding :D

Friday 8 March 2013

PLSQL check my email adress...

PL SQL Code for checking mail adress

So you want to put some security check on you're mail input field...
As you know mail can consist letters, numbers and dots....
It must contain only one @ sign...
How would you check you're email?


I do check on two different ways
No1.

Simple way of checking allowed signs in mail

function f_emailok (i_email in varchar2) return boolean is
  l_dot    NUMBER;
  l_at     NUMBER;
  l_length NUMBER;
BEGIN
  l_dot    := instr(i_email ,'.');
  l_at     := instr(i_email,'@');
  l_length := length(i_email);
  IF ((l_dot = 0) OR (l_at = 0) OR (l_dot = l_at + 1) OR
     (l_at = 1) OR (l_at = l_length) OR (l_dot = l_length))
  then
    RETURN (false);
  end if;
  IF instr(substr(i_email,l_at),'.') = 0 then
    RETURN (false);
  end if;
  return (true);
END f_emailok;

Let's explain code....

With instr function we are searching for sign in our string. In this case we search for . (dot), @ (at) and we are measuring length of our string input.
with 
   l_dot    := instr(i_email ,'.');
we get some numeric value in our l_dot variable.
let's say i_email is filled with value some.mail@gmail.com
l_dot will pick up value 5 from i_email input cause dot sign is on the fifth place in our string.
after we pick up @ value on 11th sign in string and length of  our string we can do some logic.
We say if value of l_dot is 0 or l_at = 0 or there is @ sign behind dot or @ sign is on the first place or there is all string filled with @ or with dots return false.

We still need one more check. Is there anything behind @ sign 
   instr(substr(i_email,l_at),'.') = 0
if value is zero return false...

If everything goes correctly we return true value.

But there is one bug in code :) you can pass email in incorrect format...
Figure out which and how to solve it...


No2.

Now we check email format with OWA pattern integrated function  

function f_emailok (i_email in varchar2) return boolean is
begin
         if owa_pattern.match(i_email,'\w{1,}[.,0-9,a-z,A-Z,_]\w{1,}\w{1,}[.,0-9,a-z,A-Z,_]\w{1,}'||
            '\@\w{1,}[.,0-9,a-z,A-Z,_]\w{1,}\.\w{1,}[.,0-9,a-z,A-Z,_]\w{1,}$') then
         return(true);
      else
         return(false);
      end if;
   exception
      when others then
      return(false);
END f_emailok;

So what does this code do?
It searches for allowed signs in our code....
we pass some string and in order with our owa patterns we search if it consists of illegal characters...
here is list of pattern  


PLSQL list of pattern

Assertions:

  • ^ Matches the beginning of a line (or string)
  • $ Matches the end of a line (or string)
  • Quantifiers:
  • {n,m} Must match at least n times, but not more than m times
  • {n,} Must match at least n times
  • {n} Must match exactly n times.
  • * 0 or more occurances
  • + 1 or more occurances
  • ? 0 or 1 occurance(s)

Legal atoms:

  • . matches any character except \n
  • A list of characters in square brackets [] is a class of characters,
  • for example [0-9] indicates match any character from 0 to 9.
  • \n matches newlines
  • \t matches tabs
  • \d matches digits [0-9]
  • \D matches non-digits [^0-9]
  • \w matches word characters (alphanumeric) [0-9a-z_A-Z]
  • \W matches non-word characters [^0-9a-z_A-Z]
  • \s matches whitespace characters [ \t\n]
  • \S matches non-whitespace characters [^ \t\n]
  • \b matches on “word” boundaries (between \w and \W)
  • A backslashed x followed by two hexadecimal digits, such as \x7f,matches the character having that hexadecimal value.

  • A backslashed 2 or 3 digit octal number such as \033 matches the character with the specified value.

  • Any other “backslashed” character matches itself.

PLSQL iso7064 MOD 97,10 check

What is iso7064?

Check wikipedia
It is standard for checking accounting number on Credit cards, Resident Identity Card etc....

Rules for checking Mod97,10 (iso7061) are:


This scheme produces two digits as a check. And as a result, it catches just about every possible error. If you can afford the extra digit, this system is superior to the dihedral check.
It also has an especially compact formula. The check digits are given by mod(98 - mod(data * 100, 97), 97) and the verification is just mod(data_check,97) == 1. In practice an alternate algorithm (based on Horner's Rule) is used to avoid overflow issues.
Usage: banking

 So PLSQL code should look something like this ....

function f_control_iso7064(i_input in varchar2) as
   l_input varchar2(32767) := '123456789';
   e_exception exception;
   l_control_num number(10) := 0;
begin
   -- as L_input not I
   l_input := trim(i_input);
   l_control_num := mod(98 - mod(l_input * 100, 97), 97);

   l_control_num:= mod(l_control_num,97);
   if l_control_num != 1 then
     return (false);
   else
      return (true);  
    end if;
end f_control_iso7064;

Updated post: Had some issues with the code so changed line for check.... now it's good ;)

Thursday 7 March 2013

PLSQL what day is it?

PLSQL what day is it

Say that for some reason you need name of the day for certain reason...
You can create function that will return name of the day of specified date....


function f_name_of_the day (i_date) return varchar2 as
l_day varchar2(50):=null;
begin
   SELECT TO_CHAR( trunc(to_date( to_char(to_date(i_date,'YYYYMMDD')) )),'day') into l_day FROM DUAL;
   return(day);
end f_name_of_the day ;

So what does this function do?

You enter number in date format like 20130306 which is 2013 year 03 month 06 day and function should return Wednesday value....
You can manipulate with desired input value by changing format of date
 (to_date(i_date,'YYYYMMDD')) into lets say (to_date(i_date,'YYYYDDMM')) so that input now must be in year day month shape.

CODE in SQL developer

Here is  code for simple developer test that will display name of the day

declare
l_day varchar2(50):='20130306';
begin
   select to_char( trunc(to_date( to_char(to_date(l_day,'YYYYMMDD')) )),'day') into l_day from dual;
   dbms_output.put_line(l_day);
end;

Wednesday 6 March 2013

PLSQL numeric check


PL SQL numeric check


Assuming that you need some function to check input from the front-end and you want to assure that input is numerical data, you can do it by simple function that has one input parameter and is returning false or true...
So here it goes...

Function F_numeric_val (i_value IN VARCHAR2) return boolean is
l_num NUMBER;
BEGIN
         l_num := to_number(i_value);
        return(true);
exception
   when others then
   return(false);
end;

So basically what this function do?
You have some input on front end, let's say it is 123456A and you want to check it's number consistency.
So when front-end passes you that input you call Function F_numeric_val and check its content.
In this case function will return false value to input 123456A
example;

I have program that needs to check are there any letters from third place of input till it's end.
If there are any letters I don't want to continue my code.

declare
   l_input varchar2(32767) := 'BJ1234567890        '; -- setting inputs default value
   l_errmsg varchar2(32767) := null;
begin
/* substr command means that you're checking some range of chars from desired sign. let's say we have input from l_input parameter. We string it with substr('BJ1234567890       ',3,32767) and we get  '1234567890    ' values from third sign to end of string . If we add trim before input, it will crop spaces before and after string, but not inside the string. Substr(trim('BJ1234567890       '),3,32767) this expression would return 1234567890 value but we just want to see is there any alpha characters after third sign  and we don't mind if there are any spaces so we trim them */
if not f_numeric_val(substr(trim(l_input),3,32767)) then
      l_errmsg :=( 'You don''t have number in you''re data on right places');
      return;
   else
      -- do some code here :)
      l_errmsg := 'all ok';
   end if;
exception
    when others then
       raise_application_error(-20001,'Program stopped '||sqlcode);
end;
 This code will return all ok message....
 Simple :)