Integer Variables
|
An integer, also called a natural number,
or a whole number, is a number that can start with a + or a - sign and is
made of digits. Between the digits, no character other than a digit is
allowed. In the real world, when a number is (very) long and becomes
difficult to read, such as 79435794, you are allowed to type a symbol
called the thousand separator in each thousand increment. An example is
79,435,794. In your SQL expressions, never include the thousand separator:
you would receive an error.
When the number starts with +, such as +44 or +8025, such a
number is referred to as positive and you should omit the starting + sign.
This means that the number should be written as 44 or 8025. Any number
that starts with + or simply a digit is considered as greater than 0 or
positive. A positive integer is also referred to as unsigned. On the other hand, a number that starts with a
- symbol is referred to as negative.
If a variable would hold natural numbers in the
range of -2,147,483,648 to 2,147,483,647, you can declare it with the int
or the Integer keyword as data type. Here is an example for an MSDE
statement:
DECLARE @Category int SET @Category = 208 PRINT @Category GO
If using MySQL, when declaring the variable, assign it
a natural number to indicate that the variable is an integer.
|
|
|
Decimal Variables
|
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.125 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. If you anticipate such a
number for a field, if using MSDE, declare the variable using the numeric or
the decimal
(either decimal or numeric would produce the same effect in
MSDE) data type.
If you are using MySQL, to declare a decimal variable,
assign a real number to the variable.
|
|
|
Currency Variables in MSDE
|
If a variable would hold monetary values and if using
MSDE, you can
declare it with the money or the smallmoney data type.
A variable with a money data type can hold positive or negative values from
-922,337,203,685,477.5808 to +922,337,203,685,477.5807. Here is an
example:
DECLARE @YearlyIncome Money SET @YearlyIncome = 20800.12 SELECT @YearlyIncome GO
While the money data type
can be used for a variable that would hold large quantities of currency
values, the smallmoney data type can be applied for a variable whose
value cannot be lower than -214,748.3648 nor higher than 214,748.3647.
|
Date and Time Variables
|
A datetime
data type is used for a variable whose data would consist of date and/or
time values. The entries must be valid date or time values.
In MSDE, the smalldatetime is an
alternative to the datetime data type. It follows the same rules
and principles as the datetime data type except that a date value
must be comprised between January 1st, 1900 and June 6, 2079.
To initialize a date or time-based variable, include
the desired but recognizable value in single-quotes.
|
|
|
Character Variables
|
A field of characters can consist of any
kinds of alphabetical symbols in any combination, readable or not. If you
want a variable to hold a fixed number of characters, such as the book shelf
numbers of a library, if using MSDE, you can declare it with the char data type.
In the computer world, a string is a
character or a combination of characters that are considered "as
is" with regards to the scenario in which they are used. If a
variable will hold strings of different lengths, declare it with the varchar data
type.
In some circumstances, you will need to change or
specify the number of characters used in a varchar variable. Although a First Name and a Book
Title variables should use the varchar type, both variables would not
have the same length of entries. As it happens, people hardly have a first
name that is beyond 20 characters and many book titles go beyond 32
characters. In this case, both variables would use the same data type but
different lengths. To specify the maximum number of characters that can be
stored in the variable, on the right side of varchar, type an
opening and a closing parentheses. Inside of the parentheses, type the
desired number. Here is an example:
DECLARE @Welcome varchar(50)
SET @Welcome = 'Welcome to the world of Transact-SQL'
PRINT @Welcome
GO
If you are using MySQL, simply assign a single-quoted
string to a variable to indicate that it is text-based.
No comments:
Post a Comment