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

  • SEARCH
  • Home
  • 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 6797789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:39:07+00:00 2026-05-26T18:39:07+00:00

DECLARE @T TABLE ( ID BIGINT IDENTITY PRIMARY KEY, FaceBookID BIGINT NULL, TwitterID BIGINT

  • 0
DECLARE @T TABLE  
(  
  ID BIGINT IDENTITY PRIMARY KEY,   
  FaceBookID BIGINT NULL,   
  TwitterID BIGINT NULL,  
  LinkedInID VARCHAR(50) NULL  
);  


INSERT INTO @T (FaceBookID, TwitterID, LinkedInID)  
VALUES (11111111, NULL, NULL)  

INSERT INTO @T (FaceBookID, TwitterID, LinkedInID)   
VALUES (NULL, 22222222, NULL)  

INSERT INTO @T (FaceBookID, TwitterID, LinkedInID)  
VALUES (NULL, NULL, '3333333')  


DECLARE @UserType VARCHAR(10)  
SET @UserType = 'LinkedIn'  

DECLARE @oAuthID VARCHAR(50)  
SET @oAuthID = 'aaaaaaa'  


DECLARE @UserID BIGINT  

SELECT @UserID = (  
  SELECT ID FROM @T  
    WHERE (@UserType = 'FaceBook' AND [FaceBookID] = CAST(@oAuthID AS BIGINT))  
       OR (@UserType = 'Twitter' AND [TwitterID] = CAST(@oAuthID AS BIGINT))   
       OR (@UserType = 'LinkedIn' AND [LinkedInID] = @oAuthID)  
)           

SELECT @UserID  

PROBLEM:
Even when the UserType is ‘LinkedIn’ the first WHERE clause gets executed completely and the SQL tries to CAST the value of @oAuthID, which in this case is ‘aaaaaaa’, into a BIGINT.

QUESTION:
How can I write a WHERE CLAUSE where IF the first part is NOT TRUE, the second one is NOT executed?
Ideally, because @UserType is NOT ‘FaceBook’, we should NOT try to calculate the second part of that line (the CAST).

  • 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-05-26T18:39:08+00:00Added an answer on May 26, 2026 at 6:39 pm

    While sql does do short circuiting (the feature that allows a language to only evaluate one part of a condition), it does not do it in the same way as other languages.

    Sql Server uses an optimizer to build an execution plan for a query. The optimizer looks at the query, and tries to build it in the most efficient way possible. Most of the time it optimizes for the estimated number of results or best index use: it wants to keep fewer records locked at a time and fewer records in memory at a time. But when sets from two conditions are about the same size or match similar indexes, it can also mean things like checking indexed columns in the where clause before non-indexed columns, or making small (and therefore fast) comparisons like bits or integers before longer (and therefore slower) comparisons (like string matches).

    In this case, your best chance is to treat both sides as a text type (varchar, nchar, etc) and use that for your match. It will be slower, but you must always code for correctness first, and performance second.

    This should work:

    WHERE @oAuthID = CASE WHEN @UserType = 'Facebook' THEN Cast(FaceBookID as varchar(50))
                               WHEN @UserType = 'Twitter' THEN Cast(TwitterID as varchar(50))
                               WHEN @UserType = 'LinkedIn' THEN Cast(LinkedInID as varchar(50))
                               ELSE 'invalid user type' /* could use NULL here - as long as you'll never actually see a query with this value */
                               END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

declare @T table ( ID int identity primary key, FBK_ID BIGINT null, TWT_ID BIGINT
Consider the following Transact sql. DECLARE @table TABLE(val VARCHAR(255) NULL) INSERT INTO @table (val)
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
like i declared a table... declare @tempTable TABLE (Id bigint identity(1,1), bKey int, DateT
Here is my test SQL: declare @Table table (strField nvarchar(6), xmlField xml) insert into
Consider the following sql server query , DECLARE @Table TABLE( Wages FLOAT ) INSERT
Please have a look at the below SQL code. DECLARE @RET TABLE(OID BIGINT NOT
Table is named as MasterTable Columns ID type BIGINT , Name type VARCHAR(200) (stores
I need to find the most efficient way to insert a value into table
DECLARE @cityID bigint; set @cityID = NULL --set @cityID = 3 SELECT ID, Name,

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.