Tuesday, September 15, 2009

VBScript Functions











VBScript Functions



Type Checking | Typecasting | Formatting
| Math | Date | String | Other
















 



Type Checking Functions

These functions will allow you to determine the data subtype of a
variable or expression.




















































































































































 



Value



Constant



Data Type



 



0



vbEmpty



Empty [This is the type for a variable that has yet to be used, and
is the default data type]



 



1



vbNull



Null [no valid data]



 



2



vbInteger



Integer



 



3



vbLong



Long



 



4



vbSingle



Single



 



5



vbDouble



Double



 



6



vbCurrency



Currency



 



7



vbDate



Date



 



8



vbString



String



 



9



vbObject



Object



 



10



vbError



Error



 



11



vbBoolean



Boolean



 



12



vbVariant



Variant [used with vbArray]



 



13



vbDataObject



Data Access Object



 



14



vbDecimal



Decimal



 



17



vbByte



Byte



 



8192



vbArray



Array [VBScript uses 8192 as a base for arrays and adds the code for
the datatype to indicate an array. 8204 indicates a variant array, which
is the only real kind of array in VBScript]



 




TypeName(expression) returns a
string with the name of the datatype rather than the code.



 




IsNumeric(expression) returns a
Boolean value of
True
if the
expression
is numeric, and
False
otherwise



 




IsArray(expression) returns a
Boolean value of
True
if the
expression
is an array, and
False
otherwise



 




IsDate(expression) returns a
Boolean value of
True
if the
expression
is date/time data, and
False
otherwise



 




IsEmpty(expression) returns a
Boolean value of
True
if the
expression
is an empty value (un-initialised variable), and

False
otherwise



 




IsNull(expression) returns a
Boolean value of
True
if the
expression
contains no valid data, and

False
otherwise



 




IsObject(expression) returns a
Boolean value of
True
if the
expression
is an object, and
False
otherwise
















 



Typecasting Functions

Typecasting allows you to convert between data subtypes.












































 



Cint(expression) casts
expression
to an integer. If
expression
is a floating-point value, it is rounded. If it is a string that looks
like a number, it is turned into that number and then rounded if
necessary. If it is a Boolean value of

True,
it becomes -1,
False
becomes 0. It must also be within the range that an integer can store.



 



Cbyte(expression) casts
expression
to a byte value provided that

expression
falls between 0 and 255,
expression
should be numeric or something that can be cast to a number.



 



Cdbl(expression) casts
expression
to a double.
expression
should be numeric or something that can be cast to a number.



 



Csng(expression) casts
expression
to a single. It works like

Cdbl()
but must fall within the range
represented by a single.



 



Cbool(expression) casts
expression
to a Boolean value. If

expression
is zero, the result is
False,
otherwise the result is
True.
expression
should be numeric or something that can be cast to a number.



 



Ccur(expression) casts
expression
to a Currency value.
expression
should be numeric or something that can be cast to a number.



 



Cdate(expression) casts
expression
to a Date value.
expression
should be numeric or something that can be cast to a number, or a string
of a commonly used date format.

DateValue(expression)
or
TimeValue(expression)
can also be used for this.



 



Cstr(expression) casts
expression
to a string.
expression
can be any kind of data.
















 



Formatting Functions
























 



FormatDateTime(expression, format)
is used to format the date/time data in

expression.
Format
is an optional argument that should be one of the following:



vbGeneralDate
- Display date , if present, as short date. Display time, if present, as
long time. Value is 0. This is the default setting if no format is
specified.



vbLongDate
- Display the date using the server's long date format. Value is 1.



vbShortDate
- Display the date using the server's short date format. Value is 2.



vbLongTime
- Display the time using the server's long time format. Value is 3.



vbShortTime
- Display the time using the server's short time format. Value is 4.



 



FormatCurrency(value, numdigits, leadingzero, negparen, delimiter)
is used to format the monetary value specified by

value.



numdigits
specifies the number of digits to display after the decimal place. -1
indicates to use the system default.



Tristate options have three possible values. If the value is -2, it
means use the system default. If it is -1, it means turn on the option.
If it is 0, turn off the option.



leadingzero
is a Tristate option indicating whether to include leading zeroes on
values less than 1.



negparen
is a Tristate option indicating whether to enclose negative values in
parentheses.



delimiter
is a Tristate option indicating whether to use the delimiter specified
in the computer's settings to group digits.



 



FormatNumber is used to format
numerical values. It is almost exactly like

FormatCurrency,
only it does not display a dollar sign.



FormatPercent
works the same as the two above, the options being the same except it
turns the given value into a percentage.
















 



Math Functions
















































































 



Abs(number) returns the absolute
value of
number.



 



Atn(number) returns the
arctangent, in radians, of

number
.



 



Cos(number) returns the cosine of
number.
number
should be in radians.



 



Exp(number) returns e (approx.
2.71828) raised to the power

number
.



 



Fix(number) returns the integer
portion of
number.
If
number
is negative,
Fix
returns the first integer greater than or equal to

number.



 



Hex(number) converts
number
from base 10 to a hexadecimal string.



 



Int(number) returns the integer
portion of
number.
If
number
is negative,
Int
returns the first integer less than or equal to

number.



 



Log(number) returns the natural
logarithm of
number.



 



Oct(number) converts
number
from base 10 to an octal string.



 



Rnd number returns a random number
less than one and greater than or equal to

number.



If the argument
number
is less than 0, the same random number is always returned, using

number
as a seed.

If
number
is greater than 0, or not provided,

Rnd
generates the next random number in the sequence.

If
number
is 0,
Rnd
returns the most recently generated number.



 



Randomize initialises the random
number generator.



 



Round(number) returns
number
rounded to an integer.



 



Round(number, dec) returns
number
rounded to
dec
decimal places.



 



Sgn(number) returns 1 if
number
is greater than zero, 0 if

number
equals zero, and -1 if
number
is less than zero.



 



Sin(number) returns the sine of
number.
number
should be in radians.



 



Sqr(number) returns the square
root of
number.
number
must be positive



 



Tan(number) returns the tangent of
number.
number
should be in radians.
















 



Date Functions
















































































 



Date returns the current date on
the server.



 



Time returns the current time on
the server.



 



Now returns the current date and
time on the server.



 



Year(date) returns the year
portion from
date
as a number.



 



Month(date) returns the month
portion from
date
as a number.



 



MonthName(date) returns the month
portion from
date
as a name.



 



Day(date) returns the day portion
from
date
as a number.



 



Weekday(date) returns the day of
the week of
date
as a number.



 



Hour(time) returns the hour
portion from
time.



 



Minute(time) returns the minute
portion from
time.



 



Second(time) returns the second
portion from
time.



 



DateAdd(interval, number, date) is
used to add to the date specified by

date.

interval
is a string that represents whether you want to add days, months, years
and so on.

number
indicates the number of

intervals
you want to add; that is,
the number of days, weeks, years etc.



 



DateDiff(interval, date1, date2, firstDOW, firstWOY)
is used to find the time between two dates.

interval
is one of the interval values from Table 1.

DateDiff
returns the number of
intervals
elapsed between
date1
and
date2.
The optional integer
firstDOW
specifies what day of the week to treat as the first. Values for this
can be found in Table 2. The optional integer

firstWOY
specifies which week of the year to treat as the first. Values for this
can be found in Table 3.



 



DateSerial(year, month, day) takes
the integers
year, month,
and
day
and puts them together into a date value. They may be negative.



 



TimeSerial is similar to
DateSerial.



 



Timer returns the number of
seconds elapsed since midnight.



 



DatePart(interval, datetime, firstDOW, firstWOY)
allows you to retrieve the part of

datetime
specified by
interval.
The valid values for
interval
are listed in Table 1. The optional integer

firstDOW
specifies what day of the week to treat as first. The optional integer
firstWOY
specifies which week of the year to treat as first. These values are
listed in Table 2 and Table 3.







































































Table 1: Interval codes for the Date Functions



 



Value



Meaning



 



 



"yyyy"



Year



 



"q"



Quarter



 



"m"



Month



 



"y"



Day of year



 



"d"



Day



 



"w"



Weekday



 



"ww"



Week of year



 



"h"



Hour



 



"n"



Minute



 



"s"



Second





































































Table 2: Day of the Week Constants



 



Value



Name



Meaning



 



0



vbUseSystem



National Language Support API Setting



 



1



vbSunday



Sunday (default)



 



2



vbMonday



Monday



 



3



vbTuesday



Tuesday



 



4



vbWednesday



Wednesday



 



5



vbThursday



Thursday



 



6



vbFriday



Friday



 



7



vbSaturday



Saturday













































Table 3: First Week of the Year Constants



 



Value



Name



Meaning



 



0



vbUseSystem



National Language Support API Setting



 



1



vbFirstJan1



Week of January 1



 



2



vbFirstFourDays



First week with four days of the new year



 



3



vbFirstFullWeek



First full week
















 



String Functions
















































































































 



UCase(string) returns
string
with all its lowercase letters converted to uppercase letters.



 



LCase(string) returns
string
with all its uppercase letters converted to lowecase letters.



 



LTrim(string) removes all the
spaces from the left side of

string
.



 



RTrim(string) removes all the
spaces from the right side of

string
.



 



Trim(string) removes spaces from
both the left and right of

string
.



 



Space(number) returns a string
consisting of
number
spaces.



 



String(number, character) returns
a string consisting of

character
repeated
number
times.



 



Len(string) returns the number of
characters in
string



 



Len(variable) returns the number
of bytes required by
variable.



 



LenB(string) returns the number of
bytes required to store
string.



 



StrReverse(string) returns
string
with the characters in reverse.



 



StrComp(string1, string2, comparetype)
is used to perform string comparisons. If

comparetype
is zero or omitted, the two strings are compared as if uppercase letters
come before lowercase letters. If

comparetype
is one, the two strings are compared as if upper- and lowercase letters
are the same.
StrComp
returns -1 if
string1
is less than
string2.
It returns 0 if they are the same, and 1 if

string1
is greater than
string2.



 



Right(string, number) returns the
number
rightmost characters of
string.



 



RightB(string, number) works like
Right
but
number
is taken to be the number of bytes rather than characters.



 



Left(string, number) returns the
number
leftmost characters of
string.



 



LeftB(string, number) works like
Left
but
number
is taken to be the number of bytes rather than characters.



 



Mid(string, start, length) returns
length
characters from
string,
starting at position
start.
When
length
is greater than the number of characters left in the string, the rest of
the string is returned. If

length
is not specified, the rest of
the string starting at the specified starting position is returned.



 



MidB(string, start, length) works
like
Mid,
but
start
and
length
are both taken to be byte numbers rather than character numbers.



 



InStr(start, string1, string2, comparetype)
is used to check if and where

string2
ocurrs within
string1.
start
is an optional argument that specifies where in

string1
to start looking for
string2.
comparetype
is an optional argument that specifies which type of comparison to
perform. If
comparetype
is 0, a binary comparison is performed, and uppercase letters are
distinct from lowercase letters. If

comparetype
is 1, a textual comparison is performed, and uppercase and lowercase
letters are the same.
InStr
returns 0 if
string1
is empty (" "), if
string2
is not found in
string1,
or if
start
is greater than the length of

string2
. It returns Null if either
string is Null. It returns

start
if
string2
is empty. If
string2
is successfully found in

string1
, it returns the starting
position where it is first found.



 



InStrB works like
InStr
except that the start position and return value are byte positions, not
character positions.



 



InStrRev(string1, string2, start, comparetype)
starts looking for a match at the right side of the string rather than
the left side.
start
is by default -1, which means to start at the end of the string.



 



Replace(string, find, replace, start, count, comparetype)
is used to replace ocurrences of find with

replace
in
string.
start, count
and
comparetype
are optional, but if you want to use one, you must use the ones that
come before it.
start
indicates where the resulting string will start and where to start
searching for
find.
It defaults to 1.
count
indicates how many times to perform the replacement. By default,

count
is -1, which means to replace every ocurrence. If

comparetype
is 1, a textual comparison is performed, and uppercase and lowercase
letters are the same.



 



Filter(arrStrings, SearchFor, include, comparetype)
searches an array of strings,

arrStrings
, and returns a subset of
the array.
include
is a Boolean value. if
include
is True,
filter
searches through all the strings in

arrStrings
and returns an array containing the strings that contain

SearchFor.
If
include
is False,
filter
returns an array of the strings that do not contain

SearchFor.
include
is optional and defaults to True.

comparetype
works the same as in the other string functions. If you want to use
comparetype,
you must use
include.



 



Split(expression, delimiter, count, comparetype)
takes a string and splits it into an array of strings.

expression
is the string to split up. If

expression
is zero length,
Split
returns an array of no elements.

delimiter
is a string that indicates what is used to separate the sub-strings in
expression.
This is optional; by default the delimiter is a space. If

delimiter
is zero length

(" "), an array of one element consisting of the whole string is
returned.
count
is used to specify a maximum number of sub-strings to be created. The
default for
count
is -1, which means no limit. If

comparetype
is 0, a binary comparison is performed, and uppercase letters are
distinct from lowercase letters. If

comparetype
is 1, a textual comparison is performed, and uppercase and lowercase
letters are the same.

comparetype
is only useful when the
delimiter you have chosen is a letter.



 



Join(stringarray, delimiter) does
just the opposite of
Split.
It takes an array of strings and joins them into one string, using
delimiter
to separate them.
delimiter
is optional; space being the default.
















 



Other Functions
































 



LBound(array) returns the smallest
valid index for
array.



 



UBound(array) returns the largest
valid index for
array.



 



Asc(string) returns the ANSI
character code for the first character of

string.



 



Chr(integer) returns a string
consisting of the character that matches the ANSI character code
specified by
integer.



 



Array(value1, value2, ..., valueN)
returns an array containing the specified values. This is an alternative
to assigning the values to array elements one at a time.








 


No comments: