Basic Question: Test for NULL values

# Thread: Basic Question: Test for NULL values

1. Junior Member
Join Date
Dec 1969
Posts
14

## Basic Question: Test for NULL values

For some reason I can&#039;t get the string and numeric checks to work.<BR><BR>I have two variables. One is a string and the other is a number. How do i check that they are not null. I have been using the length function but that is not reliable. Any thoughts?

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Universal test...

...that should work for most anything:<BR> If ( "" & x ) = "" Then<BR><BR>OR, if you prefer:<BR> If ( "FOO" & x ) = "FOO" Then<BR><BR>See why? If x is (a) a NULL value, (b) an EMPTY value, or (c) a string value of zero length, then appending it to an existing string will *not* change the value of that string. You might think you could do<BR> If CSTR(x) = "" Then<BR>and accomplish the same thing, but CSTR() can throw an error where string concatenation will get but ignore the error for you.<BR><BR>

3. Junior Member
Join Date
Dec 1969
Posts
21

## RE: Basic Question: Test for NULL values

If you are using vbscript, there is a function called IsNull(pass in variableName which can be string or numeric)... it returns true or false.

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Forgot to say...

...what that WON&#039;T do: It won&#039;t distinguish between a NULL (or EMPTY) value and a zero length string.<BR><BR>If you *need* to find out if something is a zero-length string, then maybe this is best:<BR> If VarType(x) = 8 AND x = "" Then<BR><BR>[I believe that the VarType for a string is 8, but you can look it up in the docs for the VarType function to be sure.]<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## To be pedantic about it...

You wrote:<BR>&#062; IsNull(pass in variableName which can be string or numeric)<BR><BR>Ummm...In VBScript all variables and values are Variants. And so NULL is actually a *different data type* than is a string or number. That is, if you do:<BR> &#060;%<BR> s = "test"<BR> Response.Write TypeName(s) & "&#060;P&#062;<BR> s = Null<BR> Response.Write TypeName(s)<BR> %&#062;<BR>you will see that in the second Write s is no longer a string data type. It&#039;s now a NULL data type.<BR>

6. Junior Member
Join Date
Dec 1969
Posts
14

## RE: Universal test...

Thanks!

7. Junior Member
Join Date
Dec 1969
Posts
21

## RE: To be pedantic about it...

Are you saying the suggestion wouldn&#039;t work?<BR><BR>And if you are in a pedantic mood, I would love your take on my question posted earlier today about getting the value(s) of a select control with multiple entries/selections. :) :) :)

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Oh, sure it works...

...just saying that ISNULL is actually testing the *TYPE* of its argument, not the value of it.<BR><BR>

9. Junior Member
Join Date
Dec 1969
Posts
21

## RE: Oh, sure it works...

Now I&#039;m confused... I see what you&#039;re saying, but from my VBScript in a Nutshell, IsNull() "Determines whether expression contains any null data". So if you set a variable&#039;s value to null, are you changing its SUBtype behind the scenes?

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## EXACTLY!

Try it yourself!<BR><BR>&#060;%<BR>V = "a string"<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR><BR>V = ""<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR><BR>V = 123<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR><BR>V = 123456<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR><BR>V = True<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR><BR>V = 1.2345678901234<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR><BR>V = Empty<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR><BR>V = Null<BR>Response.Write "V is of type " & TypeName(V) & " and has value " & V & "&#060;P&#062;"<BR>%&#062;<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•