In T-SQL, you can do this:
SELECT ProductId, COALESCE(Price, 0)
FROM Products
How do you do the same thing in Access SQL? I see examples for doing it with Nz in VBA, but I'm looking for the SQL equivalent.
Thanks.
In T-SQL, you can do this:
SELECT ProductId, COALESCE(Price, 0)
FROM Products
How do you do the same thing in Access SQL? I see examples for doing it with Nz in VBA, but I'm looking for the SQL equivalent.
Thanks.
Using
IsNull()
,Nz()
, and the data conversion functions are built-in VBA functions and will only slow down your queries in versions prior to 2003. As far as datatyping goes useCCur()
to guarantee your data type, but only if you need to do strong comparisons or simply set the format property to Currency on the column. It is the IF statement that slows things the most, as it adds yet another function to your routineusing this solution:
Nz([Price], CCur(0))
the only time
CCur()
will execute is when Price Is Null, so overall this is probably the fastest.The point is that the least number of total functions used, the faster your queries will execute.
COALESCE or NULLIF function are the standard used on sql server for a good migration to access. ISNULLor IIF or CHOOSE are nonstandard function.
Using
Iif(Price is null, 0, Price)
should give you the best performance (see Allen Browne's performance tips). However SQL ServerCoalesce()
has the great advantage overIif()
andNz()
that it can handle several parameters in a cascade. So I created this quick VBA equivalent:If it's in an Access query, you can try this:
Access supports the Nz function and allows you to use it in a query. Note though that Nz is the same as the T-SQL ISNULL function. It can not take an arbitary number of parameters like COALESCE can.
Looks like I can just use:
Seems to be working just fine.