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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:47:52+00:00 2026-06-01T02:47:52+00:00

I don’t know if this question has been ask before. But I have a

  • 0

I don’t know if this question has been ask before. But I have a problem when concating varchars.

Let me explain:

I have this table:

CREATE TABLE Table1
(
    [Field] [varchar](10) NOT NULL
)
INSERT INTO Table1
VALUES('1'),('2'),('3'),('4'),('5'),('TITLE')

And I like the output to be like this:

'[TITLE],[1],[2],[3],[4],[5]'

I want the ‘TITLE’ to be order first then 1,2,3,4,5

So this query will return the ordered result. I do not have more numbers then the length of the ‘TITLE’

SELECT
    *
FROM
    Table1
ORDER BY
    LEN([Field]) DESC,
    [Field] ASC

Then i usually concat the varchar like this:

DECLARE @cols VARCHAR(MAX)
SELECT  @cols = COALESCE(@cols + ','+QUOTENAME([Field]),
                     QUOTENAME([Field]))
FROM
    Table1
ORDER BY
    LEN([Field]) DESC,
    [Field] ASC

But this return:

'[5]'

Which I find is really strange. Can someone please explain why?

I know that there is a alternative solution to concating a varchar. Like this:

DECLARE @cols VARCHAR(MAX)
SELECT @cols=STUFF
(
    (
        SELECT 
            ',' +QUOTENAME([Field])
        FROM
            Table1
        ORDER BY 
            LEN([Field]) DESC,
            [Field] ASC
        FOR XML PATH('')
    )
,1,1,'')

This will return my expected result like this:

'[TITLE],[1],[2],[3],[4],[5]'

EDIT

Suggestion that @cols is null at the begining cant be it. Becuase if i remove the order by. Like this:

DECLARE @cols VARCHAR(MAX)
SELECT  @cols = COALESCE(@cols + ','+QUOTENAME([Field]),
                     QUOTENAME([Field]))
FROM
    Table1

My result will be like this:

'[1],[2],[3],[4],[5],[TITLE]'

EDIT1

This will not work:

DECLARE @cols VARCHAR(MAX)
SELECT  @cols = COALESCE(@cols + ','+QUOTENAME([Field]),
                     QUOTENAME([Field]))
FROM(
  SELECT [Field]
  FROM
      Table1
  ORDER BY
      LEN([Field]) DESC,
      [Field] ASC
) AS t

Because it will give a exception message like this:

Msg 1033, Level 15, State 1, Line 17 The ORDER BY clause is invalid in
views, inline functions, derived tables, subqueries, and common table
expressions, unless TOP or FOR XML is also specified.

EDIT2

Like I expected. I cannot do this:

SELECT  @cols, @cols = COALESCE(@cols + ','+QUOTENAME([Field]),
                     QUOTENAME([Field]))
FROM
    Table1
ORDER BY
    LEN([Field]) DESC,
    [Field] ASC

Becuase this will raise an exception like this:

Msg 141, Level 15, State 1, Line 9 A SELECT statement that assigns a
value to a variable must not be combined with data-retrieval
operations.

  • 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-01T02:47:53+00:00Added an answer on June 1, 2026 at 2:47 am

    You can have a look at this article PRB: Execution Plan and Results of Aggregate Concatenation Queries Depend Upon Expression Location

    The ANSI SQL-92 specification requires that any column referenced by
    an ORDER BY clause match the result set, defined by the columns
    present in the SELECT list. When an expression is applied to a member
    of an ORDER BY clause, that resulting column is not exposed in the
    SELECT list, resulting in undefined behavior.

    You get a different execution plan when you use expressions in the order by clause.

    enter image description here

    The sort is applied after Compute Scalar instead of before.

    The safe way to concatenate strings with an order by is to use for xml.

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

Sidebar

Related Questions

Don't know if this has been asked before, so point me to another question
Don't know if this has been answered before. Have custom routes to users. If
I don't know if this has been asked before, but what i'd like to
I don't know if this question is trivial or not. But after a couple
Don't know if this is the right place to ask this, but I will
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know why but I can't find a solution to this. I have 3
Don't know if I worded the question right, but basically what I want to
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know where else to ask, but from one day to the other my

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.