|Lesson 5||Data Types and Databases|
|Objective|| Coerce scripting data types to match database types.|
Data Types Databases in SQL-Server Databases
When a database is designed the columns are defined with a specific data type .
As an example, names and addresses would be character strings, quantities would probably be integers, and other values such as temperature could be floating point data types.
The data types used in script might not match that used in the database. VBScript has only one data type, called the variant type.
The variant stores all the data types and are referred to as subtypes. Additionally, parameters, or name/value pairs, passed in HTTP query strings are just that, strings. Therefore you should check data types before storing them in the database or using them in expressions such as arithmetic or comparisons. If this isn't done, errors or unpredictable results could occur.
Most, if not all, scripting languages have some level of data conversion capability. We'll look at the VBScript conversion functions in this lesson. The following table lists the data conversion functions available in VBScript.
- data type: A variable or field attribute that specifies the kind of data it can hold.
- floating point data type: A single precision number containing a sign bit and a 23-bit mantissa.
- variant: The data type for all variables that are not explicitly declared as a specific type.
- subtype: The different data types represented by the variant type.
| Function || Description || Syntax |
| Asc || Returns the ANSI character code for first character in a string || Asc(string) |
| CBool || Returns an expression converted to a Boolean || CBool(expression) |
| CByte || Returns an expression converted to a Byte || CByte(expression) |
| CCur || Returns an expression converted to subtype Currency || CCur(expression) |
| CDate || Returns an expression converted to a Date || CDate(expression) |
| CDbl || Returns an expression converted to a Double || CDbl(expression) |
| Chr || Returns the character associated with the specified character code || Chr(code) |
| CInt || Returns an expression converted to an integer || Cint(expression) |
| CLng || Returns an expression converted to a Long || Clong(expression) |
| CSng || Returns an expression converted to a Single || CSng(expression) |
| CStr || Returns an expression converted to a string || CStr(expression) |
| Hex || Returns a string representing the hexadecimal value of a number || Hex(number) |
| Oct || Returns a string representing the octal value of a number || Oct(number) |
The following script code checks to see if the variable type is an integer. If it isn't, the value is converted to an integer and stored in a new variable.
if VarType(oldvariable) <> 2 then
newvariable = CInt(oldvariable)
The VBScript VarType function returns the type stored in the variable. It returns a 2 for the integer type.
The following table outlines what you have learned about data types in this module.
Data Conversion Functions
Here are the correct matches:
| CInt || Returns an Integer type |
| CStr || Returns a String type |
| CDate || Returns a Date type |
| CByte || Returns an Byte type |
| CSng || Returns a Single type |
| CBool || Returns a Boolean type |
| CLng || Returns a Long type |
| Chr || Returns a character for the specified code |
| CDbl || Returns a Double type |
| Asc || Returns the ASCII code for a character |
In the next lesson, you will learn how to access data without using Design-Time Controls.