Decode char value to number : DECODE « Conversion Functions « Oracle PL / SQL

Home
Oracle PL / SQL
1.Aggregate Functions
2.Analytical Functions
3.Char Functions
4.Constraints
5.Conversion Functions
6.Cursor
7.Data Type
8.Date Timezone
9.Hierarchical Query
10.Index
11.Insert Delete Update
12.Large Objects
13.Numeric Math Functions
14.Object Oriented Database
15.PL SQL
16.Regular Expressions
17.Report Column Page
18.Result Set
19.Select Query
20.Sequence
21.SQL Plus
22.Stored Procedure Function
23.Subquery
24.System Packages
25.System Tables Views
26.Table
27.Table Joins
28.Trigger
29.User Previliege
30.View
31.XML
Oracle PL / SQL » Conversion Functions » DECODE 




Decode char value to number
   
SQL>
SQL> CREATE TABLE myStudent (
  2    student_id NUMBER(5NOT NULL,
  3    department CHAR(3)   NOT NULL,
  4    course     NUMBER(3NOT NULL,
  5    grade      CHAR(1)
  6    );

Table created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000'CS', 102'A');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002'CS', 102'B');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10003'CS', 102'C');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10000'HIS', 101'A');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10001'HIS', 101'B');

row created.

SQL>
SQL> INSERT INTO myStudent (student_id, department, course, grade)
  2                           VALUES (10002'HIS', 101'B');

row created.

SQL>
SQL> CREATE OR REPLACE FUNCTION AverageGrade (
  2    p_Department IN VARCHAR2,
  3    p_Course IN NUMBERRETURN VARCHAR2 AS
  4
  5    v_AverageGrade VARCHAR2(1);
  6    v_NumericGrade NUMBER;
  7    v_NumberStudents NUMBER;
  8
  9    CURSOR c_Grades IS
 10      SELECT grade
 11        FROM myStudent
 12        WHERE department = p_Department
 13        AND course = p_Course;
 14  BEGIN
 15    Debug.Reset;
 16    Debug.Debug('p_Department', p_Department);
 17    Debug.Debug('p_Course', p_Course);
 18
 19    SELECT COUNT(*)
 20      INTO v_NumberStudents
 21      FROM myStudent
 22      WHERE department = p_Department
 23      AND course = p_Course;
 24
 25    Debug.Debug('After select, v_NumberStudents', v_NumberStudents);
 26
 27    IF v_NumberStudents = THEN
 28      RAISE_APPLICATION_ERROR(-20001'No students registered for ' ||
 29        p_Department || ' ' || p_Course);
 30    END IF;
 31
 32    SELECT AVG(DECODE(grade, 'A'5,
 33                             'B'4,
 34                             'C'3,
 35                             'D'2,
 36                             'E'1))
 37      INTO v_NumericGrade
 38      FROM myStudent
 39      WHERE department = p_Department
 40        AND course = p_Course;
 41
 42    SELECT DECODE(ROUND(v_NumericGrade)5'A',
 43                                         4'B',
 44                                         3'C',
 45                                         2'D',
 46                                         1'E')
 47      INTO v_AverageGrade
 48      FROM dual;
 49
 50    RETURN v_AverageGrade;
 51  END AverageGrade;
 52  /

Function created.

SQL>
SQL>
SQL>
SQL>
SQL>
SQL> drop table myStudent;

Table dropped.

   
    
  














Related examples in the same category
1.Syntax: DECODE
2.Decode statement could be easily rewritten using the CASE statement
3.Decode for more than one key value pair
4.Use Decode as if statement to logic like 'if then else'
5.DECODE and GROUPING
6.Create a report that shows number of customers per state.
7.Use DECODE and SIGN functions on the ORD table
8.Extract any procedure, function or package
9.Decode the result from row_number over, partition by, order by
10.Decode a column with substr and instr
11.DECODE and substr function
12.Format nullable heading
13.DECODE in the GROUP BY clause
14.Demo range comparison with DECODE
15.Use decode as if statement and output 'high' or 'low'
16.Use decode create dynamic select statement
17.decode column data to output more meaningful information
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.