views:

617

answers:

2

Is it possible to set a value to values who is NULL?

The sql I use now is this:

SELECT date
FROM Activity
WHERE date BETWEEN [Forms]![Search]![fromDate] AND [Forms]![Search]![toDate]

But the problem is when there is no value for fromDate or toDate the result is zero rows. So what I want to do is when fromDate is NULL, change the fromDate value to 1800-01-01 and when toDate is NULL, change it to 2300-01-01.

+1  A: 

You should be able to use the NZ function

Nz(variant [, valueifnull ] )

NZ link

astander
A: 

Try this:

SELECT date  
FROM Activity
WHERE date BETWEEN 
    ISNULL([Forms]![Search]![fromDate], '1800-01-01') AND 
    ISNULL([Forms]![Search]![toDate], '2300-01-01')
Jojo
I have tried that, but it does not work for me. Are you sure Access support that expression?
Johan
This is T-SQL, not Jet/ACE SQL. the IsNull() function in Access/Jet/ACE only returns a Boolean if the expression evaluates to Null. The corresponding function is Nz(), i.e., NullToZero (which is not limited to numeric values, despite the name).
David-W-Fenton
@Jojo: posting answers for the wrong dialect of SQL costs you reputation points. -1.
David-W-Fenton
@David W. Fenton: Nz() is a Access object model function, not a Jet/ACE SQL function. The Jet/ACE SQL equivalent is IIF(expression_here IS NULL, true_condition_here, false_condition_here). Note this construct is superior to Nz() because it respects data types (IIRC Nz() coerces to 'string' too early).
onedaywhen
The context is Access, so the answer is correct, as it applies to THE PRESENT CONTEXT. Now, it's nice to have your comment pointing out that in other contexts other methods are required, Indeed, it would seem to me that you ought to post your own answer pointing this out, as it's a different approach than anyone else has offered and could be helpful to those trying to solve the same problem, but not in a specifically-Access context.
David-W-Fenton
@David W. Fenton: read again what you wrote. You implied that Nz() is a Jet/ACE SQL function, which is incorrect. I'm correcting your misstatement.
onedaywhen
...also Nz() is inferior even in the present context, I believe.
onedaywhen