Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:04:05+00:00 2026-06-09T05:04:05+00:00

Assume I have a T-SQL statement: select * from MyTable where Code != ‘RandomCode’

  • 0

Assume I have a T-SQL statement:

select * from MyTable
where Code != 'RandomCode'

I’ve been tasked with making this kind of where statement perform more quickly. Books Online says that positive queries (=) are faster than negative (!= , <>).

So, one option is make this into a CASE statement e.g.

select * from MyTable
where 
   case when Code = 'RandomCode' then 0 
      else 1 end = 1

Does anyone know if this can be expected to be faster or slower than the original T-SQL ?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:04:08+00:00Added an answer on June 9, 2026 at 5:04 am

    You have to be more specific at what information you are interested in the table and what are the possible values of the Code column. Then you can create appropriate indexes to speed up the query.

    For example, if values in the Code column could only be one of 'RandomCode', 'OtherCode', 'YetAnotherCode', you can re-write the query as:

    SELECT * FROM MyTable WHERE Code = 'OtherCode' OR Code = 'YetAnotherCode'
    

    And of course you need an index on the Code column.

    If you have to do an inequality query, you can change SELECT * to a more narrow query like:

    SELECT Id, Name, Whatever FROM MyTable WHERE Code != 'RandomCode'
    

    Then create an index like:

    CREATE INDEX idx_Code ON MyTable(Code) INCLUDE (Id,Name,Whatever)
    

    This can reduce I/O by replacing a table scan with an index scan.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been asked to convert this statement from PL/SQL to SQL Server: UPDATE pdi_nb_process_complete
Assume that we have this SQL statement: UPDATE article SET saison='12E', mode='ECH', client='SAS', WHERE
Have a look at the following SQL statement select * from PhoneData where names
Have a working SQL statement: my $q_it = $dbh->prepare(SELECT customdata.Field_ID, customdata.Record_ID, customdata.StringValue FROM customdata
Lets assume I have this long insert statement insert into table1 (id, name, phone,
I have some code that currently builds up an In statement in SQL. I
Lets assume I have 2 XML variables in SQL Server 2008 with the following
I have a question about SQL Server indexes. I'm not a DBA and assume
This is my use case: Input is a string representing an Oracle PL/SQL statement
Imagine the following sql query: UPDATE MYTABLE SET COL2 = (SELECT COL2 + 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.