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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:21:32+00:00 2026-05-23T12:21:32+00:00

Can COMMON Table expressions be used to avoid having SQL Server perform the following

  • 0

Can COMMON Table expressions be used to avoid having SQL Server perform the following string parsing twice per record? My guess is “no.”

SELECT DISTINCT
    Client_ID
    ,RIGHT('0000000' + RIGHT(Client_ID
                             ,PATINDEX('%[^0-9]%'
                                       ,REVERSE('?' + Client_ID)) - 1)
           ,7) AS CorrectedClient
FROM
    membob_vw
WHERE
    Client_ID <> RIGHT('0000000' + RIGHT(Client_ID
                                         ,PATINDEX('%[^0-9]%'
                                                   ,REVERSE('?' + Client_ID)) - 1)
                       ,7)
ORDER  BY
    1
    ,2 

Every time I try to format the SQL as a “Code Block” it looks good (displaying on multiple lines) until the page is refreshed, after which point the SQL is displayed , for me at least, all on ONE line- and I can’t seem to corerct that.

Does it display that way for people that are using a browser new that IE6? My company imposes this POS browser on me and prevents me for using any other.

  • 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-23T12:21:32+00:00Added an answer on May 23, 2026 at 12:21 pm

    NO, a CTE will not do anything performance wise for this query. It may seem strange/inefficient to type in the same thing large string expression twice. However, SQL Server will only do the string expression one time per row, it has been optimized for things like that.

    EDIT
    the CTE will reduce the duplicate code:

    ;WITH AllRows AS (
    SELECT DISTINCT
        Client_ID
        ,RIGHT('0000000' + RIGHT(Client_ID
                                 ,PATINDEX('%[^0-9]%'
                                           ,REVERSE('?' + Client_ID)) - 1)
               ,7) AS CorrectedClient
    FROM
        membob_vw
    )
    SELECT * FROM AllRows WHERE Client_ID<>CorrectedClient
    ORDER  BY
        1
        ,2 
    

    but won’t perform any better. USE SET SHOWPLAN_ALL ON and I’ll bet you see the same query plan for each version.

    BE CAREFUL trying to make queries look pretty and reduce redundant code fragments! simple looking SQL changes can have major adverse performance implications! always performance (run and/or query plan) check any changes you make. I have seen trivial changes made to queries that run instantly, that results in them then taking minutes to run. The key with SQL is performance not pretty code. If the application is slow, who cares if the code looks good.

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

Sidebar

Related Questions

I'm having trouble with a simple auto-increment using common table expressions. (I don't want
I'm working my way through this explanation of Common Table Expressions at MSDN and
Is it possible to do common table expressions (CTE) (like shown below) in Linq
I have the following SQL table: ObjectTable -------------------------------------------------- | ID | Name | Order
In the code below I am using a recursive CTE(Common Table Expression) in SQL
I have a table of common words that are used in sentences (i.e. A,
Can I add an INDEX to a Common Table Expression (CTE)?
I'm using the adodb php library (http://phplens.com/adodb/) to connect to sql server 2005, can
I can understand wanting to avoid having to use a cursor due to the
Is it possible to perform a JOIN sub query against a Common Table Expression?

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.