Detecting SQL Null values

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When a sql column is read that contains a null, how is that detected. A sql
null value does not appear to be a system.dbnull.value. What data type is
used to detect the presence of a sql null.

Thanks,
Fred Herring
 
Fred Herring said:
When a sql column is read that contains a null, how is that detected. A
sql
null value does not appear to be a system.dbnull.value. What data type is
used to detect the presence of a sql null.

Depending on the datatype, you can compare with 'Sql<datatype>.Null'.
 
Or get another solution from SQL query:
SELECT
CASE WHEN (Field1 IS NULL) THEN '' ELSE Field1 END AS Field1,
CASE WHEN (Field2 IS NULL) THEN 0 ELSE Field2 END AS Field2
FROM myTable
--Field1 data type is nvarchar
--Field2 data type is numeric
So "null value does not appear in your codes.
 
Or use COALESCE

Select COALESCE(Field1,'') as Field1, COALESCE(Field2,0) as Field2 FROM
myTable

From the BOL:

COALESCE
Returns the first nonnull expression among its arguments

COALESCE(expression1,...n) is equivalent to this CASE function:
CASE
WHEN (expression1 IS NOT NULL) THEN expression1
...
WHEN (expressionN IS NOT NULL) THEN expressionN
ELSE NULL
 
Is you use IsDBNull(field) it should return the results correctly for you...
 

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

Back
Top