PLSQL - Trigger for validating ISBN numbers

morph3us.org

Quicksearch

Categories

Syndication

Tagged entries

PLSQL - Trigger for validating ISBN numbers

  (Friday, December 2. 2005)
Several time ago I used the "ISBN mod 11 Check" to write a little, crappy trigger for validating ISBN numbers. It excepts a nine numbers long ISBN number as NUMBER() and the last digit of the ISBN as CHAR().
It would be possible to use "standard" ISBN formats like "0-7357-1201-8" (surreptitious advertising :oP) for the trigger too. The ISBN must/should be specified as VARCHAR2() - we can easily replace the hyphen ('-') with the replace() function (see comments in the source).

CREATE OR REPLACE TRIGGER check_isbn
  BEFORE INSERT ON buch
  FOR EACH ROW 
DECLARE
  -- isbn is nine chars long and exists of numbers only
  new_isbn NUMBER;
  new_check_digit CHAR;
  char_at NUMBER;
  summe NUMBER;
  i NUMBER;
BEGIN
  new_isbn := TO_CHAR(:new.isbn);	
  new_check_digit := TO_CHAR(:new.ISBN_Pruefziffer);
  summe := 0;
  
  -- Replace '-' to ''
  -- replace(new_isbn, '-');
  
  -- We should have 9 numbers in new_isbn
  IF NOT LENGTH(new_isbn) = 9 THEN
    RAISE_APPLICATION_ERROR(-20012, 'The ISBN number supplied is invalid!');
  END IF;
   
  -- Loop 9 times from 10 to 2
  FOR i IN REVERSE 2..10
  LOOP
    -- Get position of current number
    char_at := (11 - i);
    
    summe := summe + (i * TO_NUMBER(SUBSTR(new_isbn, char_at, 1)));
  END LOOP;

  -- Check for 'X' as last digit
  -- Add the value of 'new_check_digit' to 'sum'
  IF new_check_digit = 'X' THEN
    summe := summe + 10;
  ELSE
    summe := summe + TO_NUMBER(new_check_digit);
  END IF;
   
  IF NOT mod(summe, 11) = 0 THEN
    RAISE_APPLICATION_ERROR(-20012, 'The ISBN number supplied is invalid!');
  END IF;
END;
/
Comments (0) | Trackbacks (0)

Trackbacks

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

BBCode format allowed