![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
I'm moving some queries out of an Access front end and creating views out of them in SQL Server 2005 express. In some of the numeric fields, I use nz quite often, ( i.e. nz([MyField],0)) to return a zero if the field is null. Is there anything equivalent to this in SQL Server? Right now I'm using CASE WHEN ... but it seems like an awful lot of script to write just to replace null with a zero. Any help would be greatly appreciated. Thanks! |
#4
| |||
| |||
|
|
On Thu, 20 Apr 2006 20:25:47 GMT, "Rico" <r c o l l e n s @ h e m m i n g w a y . c o mREMOVE THIS PART IN CAPS> wrote: I'm moving some queries out of an Access front end and creating views out of them in SQL Server 2005 express. In some of the numeric fields, I use nz quite often, ( i.e. nz([MyField],0)) to return a zero if the field is null. Is there anything equivalent to this in SQL Server? Right now I'm using CASE WHEN ... but it seems like an awful lot of script to write just to replace null with a zero. Any help would be greatly appreciated. Thanks! Hi Rico, Use COALESCE: COALESCE (arg1, arg2, arg3, arg4, ...) returns the first non-NULL of the supplied arguments. You need at least two arguments, but you can add as many as you like. -- Hugo Kornelis, SQL Server MVP |
#5
| |||
| |||
|
|
Thanks Guys, I wound up finding ISNULL before I had a chance to post back. (why do I always find the solution right after I post). Is there an argument for using Coalesce over IsNull? |
#6
| |||
| |||
|
|
I wound up finding ISNULL before I had a chance to post back. (why do I always find the solution right after I post). Is there an argument for using Coalesce over IsNull? |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
On Thu, 20 Apr 2006 20:58:14 GMT, "Rico" <r c o l l e n s @ h e m m i n g w a y . c o mREMOVE THIS PART IN CAPS> wrote: Thanks Guys, I wound up finding ISNULL before I had a chance to post back. (why do I always find the solution right after I post). Is there an argument for using Coalesce over IsNull? Hi Rico, Three! 1. COALESCE is ANSI-standard and hence more portable. ISNULL works only on SQL Server. 2. COALESCE takes more than two arguments. If you have to find the first non-NULL of a set of six arguments, ISNULL has to be nested. Not so with COALESCE. 3. Data conversion weirdness. The datatype of a COALESCE is the datatype with highest precedence of all datatypes used in the COALESCE (same as with any SQL expression). Not so for ISNULL - the datatype of ISNULL is the same as the first argument. This is extremely non-standard and can cause very nasty and hard-to-track-down bugs. -- Hugo Kornelis, SQL Server MVP |
#9
| |||
| |||
|
#10
| |||
| |||
|
|
Null is not zero. Null is not a zero length string. I believe that nulls were not designed to be placeholders for these values. (snip) |
|
There may be some cases where it's a good idea to substitute a zls for a null value, but none comes to my mind at this time. |
|
IMNSHO SQL would be more rigorous if it had no IsNull(Field,Value) or corresponding Coalesce function. |
![]() |
| Thread Tools | |
| Display Modes | |
| |