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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:45:16+00:00 2026-06-14T19:45:16+00:00

I am trying to select data based on some parameters passed to my stored

  • 0

I am trying to select data based on some parameters passed to my stored procedure. I have problems with the age, I am trying to do something like this:

If my stored procedure parameter @Age = 1 then I select age between 15 to 18, @Age = 2 then 19 – 25…, apparently this is incorrect, anyone can help. Thanks.:

SELECT
    User 
FROM
    [Member] m
WHERE 
    ((m.Gender = @Gender) or @Gender IS NULL)
    and ((DATEDIFF(hour,m.DOB,GETDATE())/8766) Between 
       CASE    
         WHEN @Age = 1 THEN (SELECT DATEDIFF(hour, m.DOB, GETDATE())/8766 WHERE (SELECT DATEDIFF(hour, m.DOB, GETDATE())/8766) between 15 and 18)
         WHEN @Age = 2 THEN (SELECT DATEDIFF(hour,m.DOB,GETDATE())/8766 WHERE (SELECT DATEDIFF(hour,m.DOB,GETDATE())/8766) between 19 and 25) 
       END) 
  • 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-14T19:45:17+00:00Added an answer on June 14, 2026 at 7:45 pm

    I think this is what you are after (probably with some superfluous parenthesis):

    Select 
      [User]
    From
      [Member] m
    Where (
        (m.Gender = @Gender) or 
        @Gender Is Null
      ) And (
        (@Age = 1 And DateDiff(hour, m.Dob, GetDate())/8766 Between 15 and 18) Or
        (@Age = 2 And DateDiff(hour, m.Dob, GetDate())/8766 Between 19 and 25) 
      ) 
    

    If you’ve got a lot of clauses, it might be easier to read as (assuming a MemberID Primary Key)

    Select
      [User]
    From
      [Member] m
        Inner Join (
          Select
            MemberID,
            DateDiff(hour, m.Dob, GetDate())/8766 As Years
          From
            [Member]
        ) As y
        On m.MemberID = y.MemberID
    Where (
        (m.Gender = @Gender) or 
        @Gender Is Null
      ) And (
        (@Age = 1 And y.Year Between 15 and 18) Or
        (@Age = 2 And y.Year Between 19 and 25) 
      ) 
    

    Even better, you could add the ranges to a separate table called AgeRanges

    +-------+------------+----------+
    | AgeID | StartYears | EndYears |
    +-------+------------+----------+
    |     1 |         15 |       18 |
    |     2 |         19 |       25 |
    |   ... |        ... |      ... |
    +-------+------------+----------+
    
    Select
      [User]
    From
      [Member] m
        Inner Join
      [AgeRanges] a
        On DateDiff(hour, m.Dob, GetDate())/8766 Between a.StartYears and a.EndYears And
           a.AgeID = @Age
    

    You could also make DateDiff(hour, m.Dob, GetDate())/8766 a computed column on your members table to simplify things (and make indexing possible if performance became an issue).

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

Sidebar

Related Questions

i'm trying the following MySQL query to fetch some data: SELECT m.*, t.* FROM
I'm stuck trying to solve this. I'm plotting some information that I have in
I am trying to run a stored procedure and for some reason it keeps
I want to output some data to the user based on their username. This
Im trying to create a select command to query the database I have and
I am trying to load mysql data from my database from a html select
I am trying to select data from two tables and insert it into another
I'm currently trying to copy data from table into another by using a SELECT
In trying to select elements that have any attributes, the following throws a jQuery
What I am trying to do is create some arbitrary sql command with parameters,

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.