ISNULL

  • Comments posted to this topic are about the item ISNULL

  • "The length of the value returned is always equal to or less than the field length of the checked expression"....

    What you don't know won't hurt you but what you know will make you plan to know better
  • From BOL:

    Return Types

    Returns the same type as check_expression.

    In this case, a cast from varchar(12) to varchar(10)

  • I find these questions great training - thanks!


    RedLlewy
    "The Plural of Anecdote is not Data"

  • This is the main reason I make it a habit to use COALESCE rather than ISNULL unless there's a reason I need the behavior of ISNULL.

  • Noel McKinney (11/3/2009)


    This is the main reason I make it a habit to use COALESCE rather than ISNULL unless there's a reason I need the behavior of ISNULL.

    I prefer ISNULL because of performance.

  • Carlo Romagnano (11/3/2009)


    Noel McKinney (11/3/2009)


    This is the main reason I make it a habit to use COALESCE rather than ISNULL unless there's a reason I need the behavior of ISNULL.

    I prefer ISNULL because of performance.

    Interesting, I can't recall coming across references to performance comparisons between COALESCE and ISNULL, but yes, there does seem to be a slight advantage in CPU time with ISNULL that could be noticed over a very large number of rows. However, in my environment, preventing unintended truncation of string values is more important than slight performance gains.

  • Carlo Romagnano (11/3/2009)


    Noel McKinney (11/3/2009)


    This is the main reason I make it a habit to use COALESCE rather than ISNULL unless there's a reason I need the behavior of ISNULL.

    I prefer ISNULL because of performance.

    So, do you have any repro code that actually demonstrates a performance difference between COALESCE and ISNULL? If so, please post!

    The only situation I know of is when an expression in COALESCE is a subquery - in those cases, ISNULL can be found to perform better. But how often does one actuallly need an entire subquery in COALESCE or ISNULL?

    Like Noel, I use COALESCE rather than ISNULL. Because of the weird datatyping rules of ISNULL, but also because it supports more than two arguments, and because it's ANSI standard. The only reasons I'd ever prefer ISNULL are

    1) If I have to use it in a view or computed column where the column has to be not nullable - any COALESCE expression is always nullable; ISNULL is only nullable is the second argument is nullable.

    2) If any of the arguments has to be a subquery, because of the potential performance problems COALESCE has with those - and since this is acutally caused by an optimizer weakness, I'd make a note to check if the optimizer has finally improved in this respect after every version or servicepack upgrade.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • I've almost 'grown up' (professionally) with microsoft's sql standard, thus I'm not too familiar with ANSI standard.

    I've played with COALESCE before, but I find ISNULL is sufficient for my requirements; I haven't had to deal with multiple arguments possibly being null and I don't work with large number of rows...

    But it is good to know how to 'skin the cat' a different way

    Thanks for the input guys

  • I too go with using ISNULL because I discovered it before COALESCE.

    For multiple NULL values until I discovered COALESCE, I'd just chain ISNULL's

    ISNULL(<test value>,ISNULL(<test value 2>,<final value))

    etc.



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • Wasn't aware of this behaviour of isnull !

  • Noel McKinney (11/3/2009)


    This is the main reason I make it a habit to use COALESCE rather than ISNULL unless there's a reason I need the behavior of ISNULL.

    I wasn't aware of that ISNULL property or that COALESCE does NOT truncate that way. I've been switching to using COALESCE on occasion because I understand that it's ansi-standard, but now I have an additional reason to use COALECSE.

    Thank for a good question.

  • Thanx for the question

    1+ 🙂

    Neeraj Prasad Sharma
    Sql Server Tutorials

  • question is old, but taught me something new. thanks

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply