Results 1 to 4 of 4

Thread: [RESOLVED] SQL Case Performance Q

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Resolved [RESOLVED] SQL Case Performance Q

    If I would like to check multiple fields in my record would it be wise to use a case or is there a better method? Basically I want to check if multiple fields have a 1, if they do I am going to display 'yes' in one field.

    So it would kinda look like this

    Select ID, Sub_ID, Name,
    CASE
    WHEN Check1 = 1 THEN 'Yes'
    WHEN Check2 = 1 THEN 'Yes'
    WHEN Check3 = 2 THEN 'Yes'
    --etc...
    ELSE 'No'
    END As HasACheck
    FROM Table1

    Is there a better way that I should be doing this?

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: SQL Case Performance Q

    Code:
    Select ID, Sub_ID, Name,
    CASE WHEN Check1 = 1 OR Check2 = 1 OR Check3 = 2 THEN  'Yes'
    ELSE 'No'
    END As HasACheck
    FROM Table1
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: [RESOLVED] SQL Case Performance Q

    thanks, is that a performance increase as well as an easier way to write it?

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [RESOLVED] SQL Case Performance Q

    I'm not sure about performance increase... but surely it is easier way to write it.. you are not repeating same thing this way.

    For performance, just time the two queries in the query analyser and you should come to know. As far as I know, both should perform equally well.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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