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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:16:21+00:00 2026-05-20T10:16:21+00:00

I have a requirement to order a result set by a column, based on

  • 0

I have a requirement to order a result set by a column, based on an Integer input into a parameter.

Problem is, I need to use a CASE for the OrderBy, and it seems the code accepts the first ‘TYPE’ in the case column… any other types fail.

My code is like this:

    WITH error_table AS  
 (  
  SELECT Row_Number() OVER   
   (ORDER BY  
        CASE @orderBy
            WHEN 1 THEN received_date  -- Last Rx'd message  
            WHEN 2 THEN message_id -- Message Id  
            WHEN 3 THEN zibmat.short_name -- Message action type  
            WHEN 4 THEN error_action.short_name -- Status type  
            WHEN 5 THEN ime.[allocated_date] -- Allocated Date  
            ELSE received_date
    END) AS RowNumber   

   ,ime.[ijis_message_error_id]  
      ,ime.[message_id]  
      ,ime.[message_version] 

So, when OrderBy is 1, it works. It sorts by rx_date… but when I sent it a 2, it fails with a data time conversion error.

It looks like all the types must be the same…

Sending a 5 works fine, as that’s a date time too.

Is there a way I can fix this?

  • 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-20T10:16:21+00:00Added an answer on May 20, 2026 at 10:16 am

    A CASE statement must resolve to only one data type. This is regardless of the fact that you know that @orderby will choose only one branch and it will be a particular data type.

    You could use something like this, which would be clunky but will work.

    ORDER BY
    CASE @orderBy WHEN 1 THEN received_date -- Last Rx'd message
    WHEN 2 THEN 0
    WHEN 3 THEN 0
    WHEN 4 THEN 0
    WHEN 5 THEN ime.[allocated_date] -- Allocated Date
    ELSE received_date END,
    CASE @orderBy WHEN 1 THEN 0
    WHEN 2 THEN message_id -- Message Id
    WHEN 3 THEN 0
    WHEN 4 THEN 0
    WHEN 5 THEN 0
    ELSE 0 END,
    CASE @orderBy WHEN 1 THEN ''
    WHEN 2 THEN ''
    WHEN 3 THEN zibmat.short_name -- Message action type
    WHEN 4 THEN error_action.short_name -- Status type
    WHEN 5 THEN ''
    ELSE '' END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to be be able to embed scanned tiff images into
I have a requirement where I would not know the columns and any result
We have a requirement in project to store all the revisions(Change History) for the
I have a requirement to make a large amount of code MISRA compliant. First
We have a requirement to increase the functionality of a grid we are using
I have a requirement to be able to provide a flex component in English
We have the requirement to take a form submission and save some data, then
I have a requirement to implement an Unsaved Changes prompt in an ASP .Net
I have a requirement in my application that I think can be met by
I have a requirement on my current project (a Flex app which will be

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.