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

  • Home
  • SEARCH
  • 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 6628797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:10:07+00:00 2026-05-25T22:10:07+00:00

I am using the datediff function SELECT stName ,stId ,stDob –(varchar(15)) ,stJoinDt –(datetime) FROM

  • 0

I am using the datediff function

SELECT stName
       ,stId
       ,stDob  --(varchar(15))
       ,stJoinDt --(datetime)
FROM student stu
WHERE
DATEDIFF(yy,stu.stDob,stu.stJoinDt) between 18 and 75

Since the between operator is not effective I have also changed the code to

SELECT stName
           ,stId
           ,stDob
           ,stJoinDt
    FROM student stu
    WHERE
    DATEDIFF(yy,stu.stDob,stu.stJoinDt) >= 18 
    AND  DATEDIFF(yy,stu.stDob,stu.stJoinDt) < 75

Is there any other effective way to write datediff to capture all the missing records?

The missing records are
stDob             stJoinDt
10/08/1925        2011-01-03
04/18/1935        2011-01-19
12/11/1928        2011-06-06
1/24/1927         2011-04-18
04/18/1918        2011-04-20
  • 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-25T22:10:08+00:00Added an answer on May 25, 2026 at 10:10 pm

    Those records should be missing because the number of years between stDob and stJoinDt is not between 18 and 75, as you are filtering them out with your condition that stDob and stJoinDt differ by between 18 and 75 years:

    with student as (
        select 'Bob' as stName, 1 as stId, '10/08/1925' as stDob,        '2011-01-03' as stJoinDt
        union select 'Bob' as stName, 2 as stId, '04/18/1935',        '2011-01-19'
        union select 'Bob' as stName, 3 as stId, '12/11/1928',        '2011-06-06'
        union select 'Bob' as stName, 4 as stId, '1/24/1927 ',        '2011-04-18'
        union select 'Bob' as stName, 5 as stId, '04/18/1918',        '2011-04-20'
    )
        SELECT stName
               ,stId
               ,stDob  --(varchar(15))
               ,stJoinDt --(datetime)
               ,datediff(yy, stu.stDob, stu.stJoinDt) as DiffYears
        FROM student stu
    

    Output:

    stName  stId    stDob           stJoinDt    DiffYears
    Bob     1       10/08/1925      2011-01-03  *86* (>75)
    Bob     2       04/18/1935      2011-01-19  *76* (>75)
    Bob     3       12/11/1928      2011-06-06  *83* (>75)
    Bob     4       1/24/1927       2011-04-18  *84* (>75)
    Bob     5       04/18/1918      2011-04-20  *93* (>75)
    

    My guess would be you were wanting to capture all records where the person is at least 18 years old. In that case, remove the 75 part from the filter:

    WHERE
    DATEDIFF(yy,stu.stDob,stu.stJoinDt) >= 18 
    -- STOP HERE
    

    Although technically this does not perform the correct calculation, because it is only finding the difference in the year values and not taking into account day and month. For instance, a date-of-birth of 12/31/1990 and a join date of 1/1/2008 would register as 18 years even though the person is only 17 years, 1 day old. I would recommend instead using the solution provided in this question:

    where
        (DATEDIFF(YY, stu.stDob, stu.stJoinDt) -
            CASE WHEN( 
                (MONTH(stDob)*100 + DAY(stDob)) > (MONTH(stJoinDt)*100 + DAY(stJoinDt))
            ) THEN 1 ELSE 0 END
        ) >= 18
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scalar-valued function called DATEONLY that returns DATEADD(DD,0, DATEDIFF(DD,0, @DATETIME)), just like
This is my SQL statement that works using datediff: SELECT SUM(b134_nettpay) AS Total, b134_rmcid,
I need to calculate the year diferences between two dates. Tried using DATEDIFF but
I am using DATEDIFF in an SQL statement. I am selecting it, and I
I have the SQL statement (SQL Server ) SELECT COUNT(ActionName) AS pageCount FROM tbl_22_Benchmark
I am using a PHP variable that calculates the number of days between today
I need to calculate the DateDiff (hours) between two dates, but only during business-hours
I'm using MS SQL Server but welcome comparitive solutions from other databases. This is
I am using usort with a user comparison function to sort an array of
I'm using the following statement to generate random dates: DATEADD(day, DATEDIFF(day, 1, GETDATE()) -

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.