Problem with Len() Function & Null Values

S

scott

i've come across a real head-hurter. I'm looping through a recordset and
response.writing it's rows out with no problem except 1 field. The field
type is varchar and contains words like meeting, holiday, etc.

Problem is, I'm trying to render a "n/a" when the field is null as in
LISTING 1 below. My code isn't catching the null values. How can I test for
null values? I could swear I've successfully used the Len() test like below
successfully on similiar null varchar fields, but perhaps not.

Any ideas?

LISTING 1:

If Len(objRS(7)) < 1 Then
xTeamName= "n/a" ' this is problem line
Else
xTeamName = objRS(7)
End If
 
A

Aaron Bertrand [SQL Server MVP]

Why not do this in the query? You can use COALESCE(col, 'n/a') in SQL
Server, or NULLIF or IIF in Access.

Or, instead of the way you're doing it,

rs7 = trim(objRS(7))
if len(rs7) = 0 then rs7 = "n/a"
response.write rs7

I'm guessing there is a blank space, not a NULL value, and hence
len(objRS(7)) = 1, and falls into the else.
 
S

scott

first, what is COALESCE?

I found the culprit, I inserted zeros for null values, staying up too late
again.
 
A

Aaron Bertrand [SQL Server MVP]

first, what is COALESCE?

It is a built-in SQL Server function that takes 2 or more parameters, and
returns the first non-NULL value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top