Which is better? (SQL Server Best Practices)

  • Hi all,

    I'm new here, but not new to SQL Server, I just like to ask everyone's opinion with negation, which is better and why?

    having...

    Where LEN(Message) > 0

    OR

    Where Message IS NOT NULL

    IMHO, I prefer option 1.

  • This is not a great question.

    The two options may not give you the same result.

    It is typically better to not use a function within a join or where clause in a query because they have to be evaluated before doing the comparison - making indexes irrelevant.

  • This is similar to the SARG discussion

  • They do not produce the same results; see below:

    create table #t ( Message varchar(20), seq int not null identity(1,1))

    insert #t

    select null union all

    select '' union all

    select 'xxx'

    select * from #t where len(Message) > 0

    select * from #t where Message is not null

    Results:

    Message seq

    -------------------- -----------

    xxx 3

    (1 row(s) affected)

    Message seq

    -------------------- -----------

    2

    xxx 3

    (2 row(s) affected)

  • Better suggestion, make the app do length check and take the appopriate action.

  • Ahi va:

    Where LEN(isnull(Message, '')) > 0

    Saludos,

    José Miguel Gutiérrez Hilaccama
    Database Developer - MCTS SQL Server 2005
    No way Jose? ... bah

  • WHERE (MESSAGE IS NOT NULL) AND (MESSAGE<>'')

    This is better compare to Len function.

    * For More info read Index Topic.

  • Depending on what you are trying to achieve you can also use LIKE '_%'.

    This will exclude NULLs an empty strings '' (but not the single space strings ' ').

    Best Regards,

    Chris Büttner

  • Christian Buettner (7/16/2008)


    Depending on what you are trying to achieve you can also use LIKE '_%'.

    This will exclude NULLs an empty strings '' (but not the single space strings ' ').

    I like this answer the best, although as he says, "Depending", but in general using a function on a column in a where clause or join is a bad idea.

  • I prefer the (Where LEN(isnull(Message, '')) > 0) method.



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Here is another option that could be used:

    where isnull(Message,'') > ''

  • Another option could be an implicit varbinary conversion such as

    where message > 0x

Viewing 12 posts - 1 through 11 (of 11 total)

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