Decimal Variables
|
Introduction
|
A
decimal number is a number that can have a period (or the character used
as the decimal separator as set in the Control Panel) between the digits.
An example would be 12.625 or 44.80. Like an integer, a decimal number can
start with a + or just a digit, which would make it a positive number. A
decimal number can also start with a - symbol, which would make it a
negative number. If the number represents a fraction, a period between the
digits specifies what portion of 1 was cut.
Real Variables
|
A floating-point number
is a fractional number. To declare a variable for decimal values that do
not require too much precision, use the FLOAT or REAL data type. Here is an
example:
SQL> DECLARE Measure FLOAT := 36.12;
2 BEGIN
3 DBMS_OUTPUT.PUT_LINE('Measure: ' || Measure);
4 END;
5 /
To declare a variable for decimal values, use the NUMBER data type.
A precision is the number of digits
used to display a numeric value. For example, the number 42005 has a
precision of 5, while 226 has a precision value of 3. If the data type is specified as an integer (the
INT and its
variants) or a floating-point number (FLOAT and REAL), the precision is
fixed by the database and you can just accept the value set by the interpreter.
The scale of a number
if the number of digits on the right side of the period (or the character
set as the separator for decimal numbers for your language, as specified in Control Panel).
The scale is used only for numbers that have a decimal part.
To
control the level of precision applied on a NUMBER variable, follow the NUMBER
data type by parentheses. In the parentheses, use two values separated by a
comma. The left value represents the precision. The right value represents the
scale. The value must be an integer between 0 and 18. Here is an
example:
SQL> DECLARE Measure NUMBER(8, 3) := 284636.48; 2 BEGIN 3 DBMS_OUTPUT.PUT_LINE('Measure: ' || Measure); 4 END; 5 /
|
- Type the following:
DECLARE FirstName NVARCHAR2(28); LastName NVARCHAR2(28); FullName NVARCHAR2(60); HourlySalary NUMBER(6, 2); BEGIN FirstName := 'Patricia'; LastName := 'Katts'; FullName := LastName || ', ' || FirstName; HourlySalary := 32.85; DBMS_OUTPUT.PUT_LINE('Employee Name: ' || FullName); DBMS_OUTPUT.PUT_LINE('Hourly Salary: ' || HourlySalary); END; /
Date and Time Variables
|
A DATE data type is used for a variable whose
values would consist of date and/or
time values. The entries must be valid date or time values. The date value of a
DATE variable can be comprised between
January 1st, 4712 BC and December 31, 9999.
To initialize a DATE variable, include its
value between single-quote. For a date, use the following format:
DD-MMM-Y
or
DD-MMM-YY
or
DD-MMM-YYYY
The first number represents the day. If the number is
between 1 and 9, you can omit or include a leading 0.
The second section will contain the 3-letter name of
the month in any case of your choice (remember that SQL is not
case-sensitive).
The right section contains the value of the year:
- If the year is expressed with one digit, the year would be considered in the current decade. For example, if the year is expressed as 4 and we are in 2011, it would be considered as 2014
- If the year is expressed with 2 digits, the year would be considered in the current century. For example, if the year is expressed as 84 and we are in 2008, it would be considered as 2084
Probably to be on the safe side, you should always
express the year with 4 digits.
Here is an example:
SQL> DECLARE DateOfBirth DATE := '06-Feb-1996';
2 BEGIN
3 DBMS_OUTPUT.PUT_LINE('Date of Birth: ' || DateOfBirth);
4 END;
5 /
Date of Birth: 06-FEB-96
PL/SQL procedure successfully completed.
|
- Type the following:
DECLARE DateHired DATE; FirstName NVARCHAR2(28); LastName NVARCHAR2(28); FullName NVARCHAR2(60); HourlySalary NUMBER(6, 2); BEGIN DateHired := '06-Aug-2002'; FirstName := 'Patricia'; LastName := 'Katts'; FullName := LastName || ', ' || FirstName; HourlySalary := 32.85; DBMS_OUTPUT.PUT_LINE('Date Hire : ' || DateHired); DBMS_OUTPUT.PUT_LINE('Employee Name: ' || FullName); DBMS_OUTPUT.PUT_LINE('Hourly Salary: ' || HourlySalary); END; /
- To execute, click Run
Boolean Variables
|
A Boolean value is a piece of information stated as being true or
false. To declare a Boolean variable, use
the BOOLEAN type. Here is an example:
DECLARE IsOrganDonor BOOLEAN;
As stated previously, you can initialize the
variable when declaring. Here is an example:
DECLARE IsOrganDonor BOOLEAN := TRUE;
To initialize the variable after declaring it, in
the BEGIN...END section, access the variable and assign the
desired value. Here is an example:
DECLARE IsOrganDonor BOOLEAN; BEGIN
IsOrganDonor := TRUE;
END;
/
No comments:
Post a Comment