Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Display records based on selected date range in months within same year?

Display records based on selected date range in months within same year?

Scheduled Pinned Locked Moved Database
questionhelp
8 Posts 5 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    samflex
    wrote on last edited by
    #1

    Greetings, How do I ensure that the following code only queries records that are submitted between March and May 2024? If a record has been submitted between the above date range, display 1. Otherwise, display 0. Currently, we have a code that does this but for entire year. In the code below, I left the code that performs this check with alias of thisYear. I left this line of code but commented it out just to show what we have that works except this time, we just want this check to be between March and May 2024. The code: CASE WHEN d.dateCreated BETWEEN DATEFROMPARTS(2024, 3, 1) AND DATEFROMPARTS(2024, 5 + 1, 1) THEN 1 ELSE 0 END as thisYear appears to work sometimes but does not work other times. No errors but wrong results. That code above, temporarily replaces this line of code below as described above.

    --CASE WHEN YEAR(d.dateCreated) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear

    Here is the entire code:

     SELECT e.Name, e.email, e.emptitle, d.dateCreated,
         CASE WHEN YEAR(d.dateSubmitted) < YEAR(getdate()) THEN 1 ELSE 0 END as previousYear,
         --CASE WHEN YEAR(d.dateSubmitted) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear
     CASE WHEN d.dateSubmitted BETWEEN DATEFROMPARTS(2024, 3, 1) AND DATEFROMPARTS(2024, 5 + 1, 1) THEN 1 ELSE 0 END as thisYear
    FROM Employees e 
    INNER JOIN dateDetails d on e.employeeID = d.employeeID
    WHERE e.employeeID = someID
    

    I have also tried this:

    CASE WHEN d.dateSubmitted >= DATEFROMPARTS(2024, 3, 1) AND d.dateSubmitted < DATEFROMPARTS(2024,5 + 1, 1)

    Same inconsistent result. I guess my question is why does this work perfectly:

    CASE WHEN YEAR(d.dateSubmitted) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear

    but the date range does not work well? Thanks in advance for your help.

    J S 2 Replies Last reply
    0
    • S samflex

      Greetings, How do I ensure that the following code only queries records that are submitted between March and May 2024? If a record has been submitted between the above date range, display 1. Otherwise, display 0. Currently, we have a code that does this but for entire year. In the code below, I left the code that performs this check with alias of thisYear. I left this line of code but commented it out just to show what we have that works except this time, we just want this check to be between March and May 2024. The code: CASE WHEN d.dateCreated BETWEEN DATEFROMPARTS(2024, 3, 1) AND DATEFROMPARTS(2024, 5 + 1, 1) THEN 1 ELSE 0 END as thisYear appears to work sometimes but does not work other times. No errors but wrong results. That code above, temporarily replaces this line of code below as described above.

      --CASE WHEN YEAR(d.dateCreated) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear

      Here is the entire code:

       SELECT e.Name, e.email, e.emptitle, d.dateCreated,
           CASE WHEN YEAR(d.dateSubmitted) < YEAR(getdate()) THEN 1 ELSE 0 END as previousYear,
           --CASE WHEN YEAR(d.dateSubmitted) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear
       CASE WHEN d.dateSubmitted BETWEEN DATEFROMPARTS(2024, 3, 1) AND DATEFROMPARTS(2024, 5 + 1, 1) THEN 1 ELSE 0 END as thisYear
      FROM Employees e 
      INNER JOIN dateDetails d on e.employeeID = d.employeeID
      WHERE e.employeeID = someID
      

      I have also tried this:

      CASE WHEN d.dateSubmitted >= DATEFROMPARTS(2024, 3, 1) AND d.dateSubmitted < DATEFROMPARTS(2024,5 + 1, 1)

      Same inconsistent result. I guess my question is why does this work perfectly:

      CASE WHEN YEAR(d.dateSubmitted) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear

      but the date range does not work well? Thanks in advance for your help.

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      You didn't say what "inconsistent" means. Additionally what is the data type of 'dateSubmitted'?

      S 1 Reply Last reply
      0
      • J jschell

        You didn't say what "inconsistent" means. Additionally what is the data type of 'dateSubmitted'?

        S Offline
        S Offline
        samflex
        wrote on last edited by
        #3

        You could have easily told me if my code was right or wrong. Anyway, I have resolved it. I was actually coming here to delete the thread.

        V L J 3 Replies Last reply
        0
        • S samflex

          You could have easily told me if my code was right or wrong. Anyway, I have resolved it. I was actually coming here to delete the thread.

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          You'd be better share your solution.

          1 Reply Last reply
          0
          • S samflex

            You could have easily told me if my code was right or wrong. Anyway, I have resolved it. I was actually coming here to delete the thread.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            samflex wrote:

            I was actually coming here to delete the thread.

            Please don't. Removing some part and leaving an orphaned thread makes it difficult to know what the original question was. And many of the replies do not make sense. Just update your question with the solution you found and add the word "Solved" to the title.

            S 1 Reply Last reply
            0
            • S samflex

              You could have easily told me if my code was right or wrong. Anyway, I have resolved it. I was actually coming here to delete the thread.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              samflex wrote:

              me if my code was right or wrong.

              I wasn't sure without the information I asked for. However I can recognize that mixing date/time/stamp can be problematic. As well as not accounting for what time zone means both for programming and display.

              1 Reply Last reply
              0
              • L Lost User

                samflex wrote:

                I was actually coming here to delete the thread.

                Please don't. Removing some part and leaving an orphaned thread makes it difficult to know what the original question was. And many of the replies do not make sense. Just update your question with the solution you found and add the word "Solved" to the title.

                S Offline
                S Offline
                samflex
                wrote on last edited by
                #7

                I wasn't going to delete the thread once someone had responded to it. Here is what ultimately worked for me.

                SELECT e.Name, e.email, e.emptitle, d.dateSubmitted,
                CASE WHEN YEAR(d.dateSubmitted) < YEAR(getdate()) THEN 1 ELSE 0 END as previousYear,
                CASE WHEN d.dateSubmitted >= '20240301' AND d.dateSubmitted < '20240501' THEN 1 ELSE 0 END as thisYear
                FROM Employees e
                INNER JOIN dateDetails d on e.employeeID = d.employeeID
                WHERE e.employeeID = someID

                1 Reply Last reply
                0
                • S samflex

                  Greetings, How do I ensure that the following code only queries records that are submitted between March and May 2024? If a record has been submitted between the above date range, display 1. Otherwise, display 0. Currently, we have a code that does this but for entire year. In the code below, I left the code that performs this check with alias of thisYear. I left this line of code but commented it out just to show what we have that works except this time, we just want this check to be between March and May 2024. The code: CASE WHEN d.dateCreated BETWEEN DATEFROMPARTS(2024, 3, 1) AND DATEFROMPARTS(2024, 5 + 1, 1) THEN 1 ELSE 0 END as thisYear appears to work sometimes but does not work other times. No errors but wrong results. That code above, temporarily replaces this line of code below as described above.

                  --CASE WHEN YEAR(d.dateCreated) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear

                  Here is the entire code:

                   SELECT e.Name, e.email, e.emptitle, d.dateCreated,
                       CASE WHEN YEAR(d.dateSubmitted) < YEAR(getdate()) THEN 1 ELSE 0 END as previousYear,
                       --CASE WHEN YEAR(d.dateSubmitted) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear
                   CASE WHEN d.dateSubmitted BETWEEN DATEFROMPARTS(2024, 3, 1) AND DATEFROMPARTS(2024, 5 + 1, 1) THEN 1 ELSE 0 END as thisYear
                  FROM Employees e 
                  INNER JOIN dateDetails d on e.employeeID = d.employeeID
                  WHERE e.employeeID = someID
                  

                  I have also tried this:

                  CASE WHEN d.dateSubmitted >= DATEFROMPARTS(2024, 3, 1) AND d.dateSubmitted < DATEFROMPARTS(2024,5 + 1, 1)

                  Same inconsistent result. I guess my question is why does this work perfectly:

                  CASE WHEN YEAR(d.dateSubmitted) = YEAR(getdate()) THEN 1 ELSE 0 END as thisYear

                  but the date range does not work well? Thanks in advance for your help.

                  S Offline
                  S Offline
                  Sernjijj Kabral
                  wrote on last edited by
                  #8

                  #include #include //Whence data //asynchronous programming

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups