Results 1 to 9 of 9

Thread: Code it better

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Code it better

    I've got a table with 3 pairs of values. Both values in a pair can be null/blank. I want to return rows that have one value in a pair null/blank and one value populated. I've included what I've got but it seems kind of clunky to me. Thanks in advance for any ideas.

    Code:
       (ISNULL(PER2a,'') =  '' and ISNULL(PER2b,'') <> '')
    OR (ISNULL(PER2a,'') <> '' and ISNULL(PER2b,'') =  '')
    OR (ISNULL(PER3a,'') =  '' and ISNULL(PER3b,'') <> '')
    OR (ISNULL(PER3a,'') <> '' and ISNULL(PER3b,'') =  '')
    OR (ISNULL(PER4a,'') =  '' and ISNULL(PER4b,'') <> '')
    OR (ISNULL(PER4a,'') <> '' and ISNULL(PER4b,'') =  '')
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Code it better

    Yeah that does look a bit clunky.

    How about

    Code:
    Where (pera2a is null and pera2b is not null) or (pera2a is not null and pera2b is null)

  3. #3
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: Code it better

    maybe something like
    Code:
       coalesce(PER2a,PER2b,'itsnulla')=coalesce(PER2b,PER2a,'itsnullb')
    or coalesce(PER3a,PER3b,'itsnulla')=coalesce(PER3b,PER3a,'itsnullb')
    or coalesce(PER4a,PER4b,'itsnulla')=coalesce(PER4b,PER4a,'itsnullb')
    it should work if the key is never equal to the value and no key/value is equal to 'itsnulla'/'itsnullb' (but it's in your Hand to choose some other value to use if both are NULL)
    however, i'm not sure if its more readable than yours or datamisers...
    Last edited by digitalShaman; Nov 20th, 2015 at 02:00 AM. Reason: realized you dont want records where key and value are null so changed the last parameter for coalesce

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Code it better

    Post #2 does not work since blank and null are not treated the same (as you are doing yourself).

    #3 is more complicated, imo.

    Stick with what you have - it looks clear to me.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Code it better

    it looks clear to me
    Agreed. It's verbose but that doesn't make it unclear.

    In fact I might be tempted to make it even more verbose by removing the isnulls and testing for nulls and blanks separately:-
    Code:
    Where ((PER2a is null or PER2a = '') and PER2b is not null and PER2b != '')
    Or (PER2a is not null and PER2a != '' and (PER2b is null or PER2b = '')
    ...
    The reason I would consider that is because the use of isnull may well be preventing the optimiser from using appropriate indexes on those fields (you'd have to test it to be sure).
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  6. #6
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: Code it better

    yeah, i realized ist not that readable, but i got one more
    how about:
    Code:
    WHERE
    	(NOT (PER2a IS NULL AND PER2b IS NULL) AND PER2a+PER2b IS NULL)
    OR  (NOT (PER3a IS NULL AND PER3b IS NULL) AND PER3a+PER3b IS NULL)
    OR  (NOT (PER4a IS NULL AND PER4b IS NULL) AND PER4a+PER4b IS NULL)
    but the concat might make it slow on big tables...

    arghh, it would Need extra stuff for '', so gets more complicated again
    stick with what you have :P

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Code it better

    Quote Originally Posted by FunkyDexter View Post
    ...The reason I would consider that is because the use of isnull may well be preventing the optimiser from using appropriate indexes on those fields (you'd have to test it to be sure).
    Not sure indexes and nulls mix in real life much...

    What I like about your code is that it's not using any SCALAR functions - just EQUAL and NOT EQUAL stuff which is so compile-able into clear operational instructions to the engine.

    Not like the speed involved here would make a difference...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Code it better

    Quote Originally Posted by wild_bill View Post
    I've got a table with 3 pairs of values. Both values in a pair can be null/blank. I want to return rows that have one value in a pair null/blank and one value populated. I've included what I've got but it seems kind of clunky to me. Thanks in advance for any ideas.
    ...
    I'm not a database person, but this sounds like it might be an XOR situation.
    Can both values be populated, and do you want to return the row if they are, or only return the row when they are different (i.e one null and one not).
    XOR returns True when the two values are different.
    False Xor False = False
    True Xor True = False
    False Xor True = True
    True Xor False = True
    Code:
       (ISNULL(PER2a,'') =  '' xor ISNULL(PER2b,'') = '')
    OR (ISNULL(PER3a,'') =  '' xor ISNULL(PER3b,'') = '')
    OR (ISNULL(PER4a,'') =  '' xor ISNULL(PER4b,'') = '')
    I'm assuming the code above is not valid syntax for a query, but is the principle.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Code it better

    Seems that T-SQL does not have an XOR logical operator - people try to trick it up with the BITWISE XOR of "^" (caret).

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width