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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:58:53+00:00 2026-05-25T06:58:53+00:00

I have the following query. When run I receive Error converting data type nvarchar

  • 0

I have the following query. When run I receive

Error converting data type nvarchar to numeric.

This only occurs when @sortorder = 1 or -1, sorted by name. However, if the case statement is removed and just order by Name asc is used the query run without error.

Could anyone explain whats going on here? thanks.

DECLARE @SortOrder INT = 1

CREATE TABLE #Results
(
ID INT IDENTITY(1,1),

NameID INT,

Expected NUMERIC(5,1),

Actual NUMERIC(5,1)

)

SELECT Name, Expected, Actual, ID 

FROM (

      SELECT ISNULL(NULLIF(Words.Word, ''), Name.Primary) AS [Name], #Results.Expected, #Results.Actual, #Results.NameID 
    FROM #Results
    INNER JOIN Name ON Name.ID  = #results.NameID 
    LEFT OUTER JOIN Words ON Name.WordID = Words.WordID 
        AND Words.LanguageID = 0
    UNION
    SELECT 'AVG' AS Name,
            CAST(((SUM(#Results.Expected))/COUNT(#Results.ID)) AS NUMERIC(5,1)) AS [Expected],
            CAST(((SUM(Actual))/COUNT(ID)) AS NUMERIC(5,1))AS [Actual],
            -9999 as [ID]
    FROM #Results 
) AS Results

ORDER BY 
    CASE @SortOrder

        WHEN 1 THEN Name    
        WHEN 2 THEN Expected
        WHEN 3 THEN Actual      
        END ASC,    
    CASE @SortOrder 
        WHEN -1 THEN Name   
        WHEN -2 THEN Expected
        WHEN -3 THEN Actual
        END DESC
  • 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-25T06:58:54+00:00Added an answer on May 25, 2026 at 6:58 am

    As a general rule, don’t ever use something like that. Use three separate queries:

    if @SortOrder = 1 then
    select ... order by Name;
    else if @SortOrder = 2 then
    select ... order by Expected
    else if @SortOrder = 3 then
    select ... order by Actual;
    

    What you’re doing now is the 100% sure way to deny the SQL Server query optimizer any decent chance to come up with an efficient plan: the generated plan has to work for any value of the @SortOrder variable, therefore it must do a sort even if an index would satisfy the order by constraint. For a more lengthier discussion on the topic read Dynamic Search Conditions in T-SQL.

    Now in your case the data coming from a #temp table is even more important to separate the cases higher up the stack. Since #temp tables support indexes, add the appropriate clustered index to the #temp table itself when you create it. Which implies using specific, separate, #temp tables for Name, Expected or Actual order by requirement.

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

Sidebar

Related Questions

I have the following query, now the strange thing is if I run this
I have the following Query and i need the query to fetch data from
I have following complex query which I need to use. When I run it,
I have the following CAML query: <Where> <And> <Eq> <FieldRef Name='PublishToSM' /> <Value Type='Boolean'>True</Value>
When I run my script I receive the following error before processing all rows
I have following query, and I am converting it to LINQ. select acq.ACQPub as
If I have the following query, is it possible to be able to run
Say I have the following query OData Linq Query (run against http://odata.netflix.com/v2/Catalog ): Genres.Where(x=>x.Name==Adventures)
I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT c.* FROM companies AS c JOIN users AS

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.