How do I achieve exclusive OR in T-SQL?

12,350

Solution 1

Here's one solution I've found:

select top 1 *
from accounts
where usr_key = @specified_user
order by
  acc_active desc,
  case 
    when getdate() between acc_effective and acc_expires then 0
    else 1
  end,
  acc_expires desc

This would effectively order the records in the right priority sequence allowing me to pick the top one off the list

Strictly speaking, it doesn't achieve exclusive or, but it could be applied to this data set to achieve the same end.

Solution 2

Unless something has radically changed in TSQL 2008, it's brute force.

select *
from table
where (    ( condition 1 OR condition 2)
    AND NOT ( condition 1 AND condition 2)    )
Share:
12,350
BenAlabaster
Author by

BenAlabaster

Tinkerer, problem solver, artist, photographer and professional code monkey. Full time software engineer, father and student desperately seeking more hours in the day so I can keep up with life. I'm currently a free agent and looking for my next project, so if you are in need of a strong and dedicated .NET developer, please get in touch. I can be found in the following places: Google Plus: http://gplus.to/BenAlabaster Twitter: @BenAlabaster Blog: http://www.endswithsaurus.com/ LinkedIn: http://www.linkedin.com/in/BenAlabaster Email: BenAlabaster at gmail dot com

Updated on June 13, 2022

Comments

  • BenAlabaster
    BenAlabaster about 2 years

    I have a data table where there's a list of columns (boiled down to the pertinent ones for this example):

    users(
      usr_pkey int identity(1, 1) primary key,
      usr_name nvarchar(64),
      ...,
    )
    
    accounts(
      acc_pkey int identity(1, 1) primary key,
      usr_key int foreign_key references users(usr_pkey),
      acc_effective datetime,
      acc_expires datetime,
      acc_active bit,
      ...,
    )
    

    From this table I'm looking to grab all records where:

    • The account belongs to the specified user and
    • In the first instance:
      • the account is active and today's date falls between the account's effective and expiry date or
    • In the second instance:
      • if no records were identified by the first instance, the record with the most recent expiry date.

    So - if an active record exists where today's date falls between the account's effective and expiry dates, I want that record. Only if no match was found do I want any account for this user having the most recent expiry date.

  • Philip Kelley
    Philip Kelley about 13 years
    I'd do it as a subquery, but otherwise yes, "selecting all", ordering appropriately, and picking the top one probably (depending on table structure, row count, and indexing) is the best way to do this.
  • BenAlabaster
    BenAlabaster about 13 years
    @Philip Kelley In this instance, it's a small scale app for half a dozen users with a fairly limited dataset - probably no more than tens of thousands of records at the outside, so I think this will be suitable.