dbTalk Databases Forums  

Any functions to replace NZ in SQL Server?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Any functions to replace NZ in SQL Server? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rico
 
Posts: n/a

Default Any functions to replace NZ in SQL Server? - 04-20-2006 , 03:25 PM






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!



Reply With Quote
  #2  
Old   
SQL
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-20-2006 , 03:31 PM






use coalesce or isnull

declare @v int
select coalesce(@v,0),isnull(@v,0)


Denis the SQL Menace
http://sqlservercode.blogspot.com/


Reply With Quote
  #3  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-20-2006 , 03:38 PM



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:

Quote:
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


Reply With Quote
  #4  
Old   
Rico
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-20-2006 , 03:58 PM



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?

Thanks!



"Hugo Kornelis" <hugo (AT) perFact (DOT) REMOVETHIS.info.INVALID> wrote

Quote:
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



Reply With Quote
  #5  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-20-2006 , 04:09 PM



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:

Quote:
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


Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-20-2006 , 04:12 PM



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)
writes:
Quote:
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?
In theory, coalesce is what you always should use, because:

1) It's ANSI-compatible.
2) coalesce can accept list of several values, whereas isnull accepts
exactly two.

Unfortunately, there are contexts were isnull() is preferable, or the
only choice. The ones I'm thinking of are:
1) In definition of indexed views you may need to use isnull to make
the view indexable.
2) I've seen reports where using coalesce resulted in a poor query plan
whereas isnull did not. I should add that that was not really a plain-
vanilla query.

So despite these excpetions, I would recommend coalesce. Even if it's
more difficult to spell.
--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #7  
Old   
Lyle Fairfield
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-20-2006 , 05:57 PM



Null is not zero. Null is not a zero length string.

I believe that nulls were not designed to be placeholders for these
values.
We should be extremely careful when we convert nulls to values. Such
conversion could lead to error. Often it is persons without strong
grounding in mathematics and logic who make these conversions,
increasing the likelihood of such error. The best practice is likely to
be the exclusion of records with nulls in the columns we are processing
and to enter values in those where a value is appropriate. 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.

[Yes, I've probably posted IsNull(Field,Value) solutions here; that was
then; this is now.]


Reply With Quote
  #8  
Old   
Rico
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-20-2006 , 06:00 PM



Excellent! Thanks!


"Hugo Kornelis" <hugo (AT) perFact (DOT) REMOVETHIS.info.INVALID> wrote

Quote:
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



Reply With Quote
  #9  
Old   
Madhivanan
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-21-2006 , 05:48 AM



Read about IsNull Vs Coalesce
http://www.sqlservercentral.com/colu...weenisnull.asp

Madhivanan


Reply With Quote
  #10  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Any functions to replace NZ in SQL Server? - 04-21-2006 , 03:11 PM



On 20 Apr 2006 15:57:53 -0700, Lyle Fairfield wrote:

Quote:
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)

Hi Lyle,

Thus far, I agree with yoour post.

(snip)
Quote:
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.
First, you should be awarer that COALESCE and ISNULL on SQL Server, or
Nz on Access, can not just be used to replace NULL with 0 or zero length
string - you can replace them with anything you like. Common uses are
COALESCE (SomeColumn, 'n/a') in a report. Or
COALESCE (UserSpecifiedColumn, DefaultValue) in any query or view.

Quote:
IMNSHO SQL would be more rigorous if it had no IsNull(Field,Value) or
corresponding Coalesce function.
I disagree with this statement. As I've shown above, COALESCE and ISNULL
can be used in very useful ways. That they might also be abused by
people who fail to think their solutions through is sad, but no reason
to abolish them. That's like forbidding cars because someone might cause
an accident while drinking and driving.

Besides, since COALESCE is just a shorthand for a specific CASE
expression, removing COALESCE from the language would have no effect;
people would just use the equivalent CASE expression.

--
Hugo Kornelis, SQL Server MVP


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.