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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:09:25+00:00 2026-05-23T19:09:25+00:00

I have a select which needs to have dynamic WHERE CASE conditions. SELECT *

  • 0

I have a select which needs to have dynamic WHERE CASE conditions.

SELECT 
      *
FROM 
      vwShowUctyPohyby
WHERE
      (@id_parent is null or id_parent=@id_parent)
      and (datetime_creation <= @record_first_datetime)
ORDER BY 
      CASE @sort_order WHEN 0 THEN datetime_creation END ASC, 
      CASE @sort_order WHEN 1 THEN datetime_creation END DESC  

As you can see there is a condition to dynamically choose type of ORDER. Now I need to do same with WHERE part. I found that it can work like datetime_creation = CASE ….. WHEN … THEN …. END.

Problem is that I need to create datetime_creation <= @record_first_datetime for one WHERE condition and datetime_creation >= @record_first_datetime for other condition.

How do I do that? I dont want to create two selects and use IF ELSE statement.

I am using this SELECT for dynamic paging on SQL side, with ROWCOUNT.

  • 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-23T19:09:25+00:00Added an answer on May 23, 2026 at 7:09 pm

    Is this your intention ?

    SELECT  * 
    FROM  vwShowUctyPohyby 
    WHERE 
          (@id_parent is null or id_parent=@id_parent) 
          and 
          ((datetime_creation <= @record_first_datetime and @sort_order = 0) or
          (datetime_creation >= @record_first_datetime and @sort_order = 1))
    ORDER BY  
          CASE @sort_order WHEN 0 THEN datetime_creation END ASC,  
          CASE @sort_order WHEN 1 THEN datetime_creation END DESC   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a select from table which does not have column that tells when
I have a select list which is being populated using the values from a
Let's say I have SELECT name FROM table which gives me something like foo
I have this hql query, which works perfect: select m from Media m join
I have a table with an order column which needs to maintain a contiguous
I have a simple application in which I need to let the user select
I have a select box which has more than 200 items. Below the select
I have a jQuery select which works in most browsers axcept IE. The code
I have a SQL SELECT query which has a LIKE clause containing an underscore,
I have a datagridview where the users can select which subset of columns to

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.