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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:00:53+00:00 2026-06-01T13:00:53+00:00

I have a fairly simple pagination query used to get rows from a table

  • 0

I have a fairly simple pagination query used to get rows from a table

ALTER PROCEDURE mytable.[news_editor_paginate]
    @count int,
    @start int,
    @orderby int
AS
BEGIN
    SET NOCOUNT ON;
    SELECT TOP (@count) * FROM 
    (  
        SELECT news_edits.*,
        ROW_NUMBER() OVER (
            ORDER BY CASE 
                WHEN @orderby = 0 THEN news_edits.[time]
                WHEN @orderby = 1 THEN news_edits.lastedit
                WHEN @orderby = 2 THEN news_edits.title
                END
            DESC
        ) AS num
        FROM news_edits
    ) AS a
    WHERE num > @start
END

The @orderby parameter decides which column the results should be ordered by.

news_edit.[time] and news_edits.lastedit are both datetime fields. But news_edits.title is a varchar field.

The query runs fine for both the datetime fields but when @orderby = 2 I get the following error:

“Conversion failed when converting date and/or time from character string.”

The problem I’m having is that I’m not trying to convert anything?

  • 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-01T13:00:55+00:00Added an answer on June 1, 2026 at 1:00 pm

    You’ll need to divide your ORDER BY into multiple CASE statements:

    ORDER BY 
        CASE WHEN @orderby = 0 THEN news_edits.[time] END DESC,
        CASE WHEN @orderby = 1 THEN news_edits.lastedit END DESC,
        CASE WHEN @orderby = 2 THEN news_edits.title END DESC
    

    This is because single CASE statement requires that all branches have compatible data types. Since your character string in one CASE can’t be converted to the date time returned from another CASE, you get the conversion error.

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

Sidebar

Related Questions

I have a fairly simple Linq query (simplified code): dim x = From Product
I have a fairly simple SQL statement to get some results from a couple
I have a fairly simple cell in a table with an inline style: <td
I have a fairly simple query that runs super fast most of the time
I have a fairly simple requirement for a query API which I need to
I have a fairly simple android app, it gets protobuf data from a web
I have a fairly simple sync problem. I have a table with about 10
I have a fairly simple table requestparams ( requestid varchar(64) NOT NULL, requestString text,
I have a fairly simple dynamic html structure of a table that contains links
I have a fairly simple table (forgive errors / stupidity, I'm still learning. Written

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.