TechTalk: Replacing a null value with a default value in an SQL statement.

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Q: How do I get rid of null values in my SQL query result set? I want to retrieve the records with null values, but I would like to put a default value in the field instead of just receiving NULL.

A: The SQL VALUE function will handle this nicely. The function will return the first value that is not null from a list of values given to it. So, to make it return a default value when a field is NULL, use the following syntax:

 Select Field1, VALUE(Field2,'Default') from File 

Now, whenever Field2 is NULL, you will get the value "Default" in the field. You can also use this technique to avoid getting null results in SQL outer joins when there is no matching record.

- Brian Singleton

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$