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 1108677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:07:18+00:00 2026-05-17T02:07:18+00:00

I seem to be facing a strange issue in SQL 2008. I have a

  • 0

I seem to be facing a strange issue in SQL 2008.

I have a query which runs fine and fast from query analyser, but times out if run through a stored procedure! The SP just starts with this query and has no other code before this query

SELECT col1,col2 FROM TBL1 (nolock)
INNER JOIN TBL2 (nolock) 
ON tbl1.col=LEFT(tbl2.col1,LEN(tbl2.col1)-2) AND tbl1.col2=RIGHT(tbl2.col1,2)
AND tbl1.col4=2233
AND tbl1.date1 BETWEEN tbl2.date1 and isnull(tbl2.date2,getdate())

Please note that tbl1 is actually a view, where the col and col2 are coming via a self join. Also as per business requirement, tbl2.col1 needs to have concatenated value. If required to solve this issue, I can modify my view though.

  • 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-17T02:07:19+00:00Added an answer on May 17, 2026 at 2:07 am

    As a side issue, please note that if you can make some assumptions about string lengths, your join expression can be simplified (and possibly get better performance because one side is now using equality):

    SELECT tbl1.col1, tbl1.col2
    FROM
       TBL1
       INNER JOIN TBL2
    
          ON tbl1.col + tbl1.col2 = tbl2.col1
          AND tbl1.col4=2233
          AND tbl1.date1 BETWEEN tbl2.date1 AND Coalesce(tbl2.date2, GetDate())
    

    Also, if you’re looking for the best performance, try this:

    ALTER TABLE TBL2 ADD LeftPart AS (LEFT(col1, LEN(col1)-2));
    ALTER TABLE TBL2 ADD RightPart AS (RIGHT(tbl2.col1,2));
    CREATE NONCLUSTERED INDEX IX_TBL2_Parts ON TBL2 (LeftPart, RightPart);
    

    Now you can just join like so:

    SELECT tbl1.col1, tbl1.col2
    FROM
       TBL1
       INNER JOIN TBL2
          ON tbl1.col = tbl2.LeftPart
          AND tbl1.col2 = tbl2.RightPart
          AND tbl1.col4=2233
          AND tbl1.date1 BETWEEN tbl2.date1 AND Coalesce(tbl2.date2, GetDate())
    

    Even better, change your database design to actually store the TBL2.col1 data in two columns. You’re violating first normal form by putting two distinct pieces of data in one column, and now, as you’re discovering, you’re paying for it throughout your application in terms of performance, development & maintenance time, query complexity, and so on.

    You could even reverse my scheme so that the LeftPart and RightPart columns are real, and you create a new calculated column that has the Col1 name, with an index to materialize the values and make them searchable. Finally, if absolutely required, you could rename the table, create a view on the table using the old name, and then put an INSTEAD-OF triggers on the view to intercept data operations against the table and translate them into the correct schema.

    Update

    By the way, if you have any influence on table design you may want to consider using an “open ended date” value of ‘99991231’ or some such for tbl2.date2 rather than NULLs. That Coalesce can kill performance, sometimes forcing a scan when a seek would have been possible.

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

Sidebar

Related Questions

I seem to always have problems with converting data to and from XML in
There seem to many ways to skin this particular cat - but which is
Seem to be having an issue with std::auto_ptr and assignment, such that the object
I seem to be getting a strange error when I run my tests in
I am facing a strange FlexUnit Error: Whoa... been asked to send another complete
I'm facing a problem where both recursion and using a loop seem like natural
I'm facing a new challenge here. I can't seem to find precedence for replication
I am learning C language and I am facing a problem I don't seem
I'm facing an issue with merging a branch back into a tree on SVN,
the problem I'm facing is this: I have a textbox for a date range

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.