URGENT T- SQL QUESTION. NEED HELP.

  • Mr. Kapsicum (1/30/2013)


    Thanks uravindarreddy,,, Ur query is Great.

    But Can U explain its working to me.,? I m a bit naive to SQL , cant understand its in depth working.!!

    Thanks again for ur help.

    The COALESCE function uses the first non null field in the list, which makes it effectively an easy to read Nested ISNULL

    eg

    ISNULL(Col,ISNULL(col2,ISNULL(col3,'')))

    Can be replaced with

    COALESCE(Col,col2,col3,'')

    Which means that if Col is NULL then it checks Col2, which if that is NULL then it will check col3 will be used, and finally if all 3 are NULL then it will use ''.

    The final point COALSECE is more readable than the nested ISNULL especially if you have 4/5 columns that you want to check in a specific order.

    Though I'm sure I read somewhere there can be some interesting sideffects of using COALESCE, though I cant remember wher I read it.

    One thing to remember is that it will probably cause the optimiser to chose an inefficient query plan.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • Jason-299789 (1/30/2013)


    Sorry I spotted that, and just changed them to work. Theres still a problem with the ISNULL though.

    this is the isnull

    SELECT ID,COLOR

    FROM #TBLCOLOR

    WHERE ISNULL(COLOR,'')=ISNULL(@COLOR,ISNULL(COLOR,''))

    Just bear in mind that can't use indexes, so that's a table scan every time.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (1/30/2013)


    Jason-299789 (1/30/2013)


    Sorry I spotted that, and just changed them to work. Theres still a problem with the ISNULL though.

    this is the isnull

    SELECT ID,COLOR

    FROM #TBLCOLOR

    WHERE ISNULL(COLOR,'')=ISNULL(@COLOR,ISNULL(COLOR,''))

    Just bear in mind that can't use indexes, so that's a table scan every time.

    Very true, but the OP wanted to see how to do this using an ISNULL. My prefered option in this case would be WHERE Colour=@Colour or @Colour is NULL.

    On a more complex where you have several of those type clauses then a 'Catch all' query is most likely the better option.

    _________________________________________________________________________
    SSC Guide to Posting and Best Practices

  • Jason-299789 (1/30/2013)


    Mr. Kapsicum (1/30/2013)


    Thanks uravindarreddy,,, Ur query is Great.

    But Can U explain its working to me.,? I m a bit naive to SQL , cant understand its in depth working.!!

    Thanks again for ur help.

    The COALESCE function uses the first non null field in the list, which makes it effectively an easy to read Nested ISNULL

    eg

    ISNULL(Col,ISNULL(col2,ISNULL(col3,'')))

    Can be replaced with

    COALESCE(Col,col2,col3,'')

    Which means that if Col is NULL then it checks Col2, which if that is NULL then it will check col3 will be used, and finally if all 3 are NULL then it will use ''.

    The final point COALSECE is more readable than the nested ISNULL especially if you have 4/5 columns that you want to check in a specific order.

    Though I'm sure I read somewhere there can be some interesting sideffects of using COALESCE, though I cant remember wher I read it.

    One thing to remember is that it will probably cause the optimiser to chose an inefficient query plan.

    Mr.Jason;

    SELECT * FROM TBLCOLOR

    WHERE COALESCE (COLOR, '') = COALESCE (@COLOR, COLOR, '');

    in this query coalesce(@color,color,'')

    the field color is always not null then how can we compare ''.?

    and which is more efficient ISNULL or Coalesce.? πŸ˜‰ Plz Help

  • Mr. Kapsicum (1/30/2013)


    ...

    i want my query to use BOTH ISNULL and COALESCE...

    Only homework would impose such a restriction.

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Mr. Kapsicum (1/30/2013)


    Mr.Jason;

    SELECT * FROM TBLCOLOR

    WHERE COALESCE (COLOR, '') = COALESCE (@COLOR, COLOR, '');

    in this query coalesce(@color,color,'')

    the field color is always not null then how can we compare ''.?

    and which is more efficient ISNULL or Coalesce.? πŸ˜‰ Plz Help

    Neither. They're both going to suck because you have wrapped a function aroud a column name in the WHERE clause. Go back and look at Gila Monster's post for confirmation of that. Then go look at the article for "SQL in the Wild" (by Gila Monster again) that was posted early on in the game for the right way to do this.

    You asked for "urgent" help. I urgently suggest you find that link and read it. πŸ˜‰

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ChrisM@Work (1/30/2013)


    Mr. Kapsicum (1/30/2013)


    ...

    i want my query to use BOTH ISNULL and COALESCE...

    Only homework would impose such a restriction.

    Yep; I'm sure of that too. 100% homework (or bad interview type of q).

    SQL DBA,SQL Server MVP(07, 08, 09) A socialist is someone who will give you the shirt off *someone else's* back.

Viewing 7 posts - 16 through 21 (of 21 total)

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