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. Querying on null datetime

Querying on null datetime

Scheduled Pinned Locked Moved Database
databasedesignhelp
8 Posts 5 Posters 0 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.
  • W Offline
    W Offline
    www Developerof NET
    wrote on last edited by
    #1

    Hi, I have a table which has a date column "OrderedDate" which can be null in db. Secondly i have a UI which has an frmdate and todate. While querying if the two textboxes are empty i need to show all the records( including the rows with null date). I have written the following query but it fetches the ros with non null values only.

    AND
    (
    isnull(OrderedDate,@frmdate) between isnull(@frmdate,OrderedDate) and isnull(@todate,OrderedDate)
    )

    I can`t figure out whats the problem. Kindly look into the matter if possible. Thank You.

    When you fail to plan, you are planning to fail.

    L L L P 6 Replies Last reply
    0
    • W www Developerof NET

      Hi, I have a table which has a date column "OrderedDate" which can be null in db. Secondly i have a UI which has an frmdate and todate. While querying if the two textboxes are empty i need to show all the records( including the rows with null date). I have written the following query but it fetches the ros with non null values only.

      AND
      (
      isnull(OrderedDate,@frmdate) between isnull(@frmdate,OrderedDate) and isnull(@todate,OrderedDate)
      )

      I can`t figure out whats the problem. Kindly look into the matter if possible. Thank You.

      When you fail to plan, you are planning to fail.

      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      You can construct separate queries for cases when the text boxes are empty.

      1 Reply Last reply
      0
      • W www Developerof NET

        Hi, I have a table which has a date column "OrderedDate" which can be null in db. Secondly i have a UI which has an frmdate and todate. While querying if the two textboxes are empty i need to show all the records( including the rows with null date). I have written the following query but it fetches the ros with non null values only.

        AND
        (
        isnull(OrderedDate,@frmdate) between isnull(@frmdate,OrderedDate) and isnull(@todate,OrderedDate)
        )

        I can`t figure out whats the problem. Kindly look into the matter if possible. Thank You.

        When you fail to plan, you are planning to fail.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Your logic seems flawed; I would try

        AND
        (
        @frmdate IS NULL OR @todate IS NULL OR OrderedDate BETWEEN @frmdate AND @todate
        )

        warning: BETWEEN is inclusive or exclusive, depending on the actual database; when uncertain use explicit comparison with >= and <= :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Prolific encyclopedia fixture proof-reader browser patron addict?
        We all depend on the beast below.


        1 Reply Last reply
        0
        • W www Developerof NET

          Hi, I have a table which has a date column "OrderedDate" which can be null in db. Secondly i have a UI which has an frmdate and todate. While querying if the two textboxes are empty i need to show all the records( including the rows with null date). I have written the following query but it fetches the ros with non null values only.

          AND
          (
          isnull(OrderedDate,@frmdate) between isnull(@frmdate,OrderedDate) and isnull(@todate,OrderedDate)
          )

          I can`t figure out whats the problem. Kindly look into the matter if possible. Thank You.

          When you fail to plan, you are planning to fail.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          While I already answered your actual question, what I would really do myself is:

          AND
          (
          @frmdate IS NULL OR OrderedDate >= @frmdate
          }
          AND
          (
          @todate IS NULL OR OrderedDate <= @todate
          )

          That allows you to leave either one empty, so you can get: - all - all up to some date - all since some date - all in an interval :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Prolific encyclopedia fixture proof-reader browser patron addict?
          We all depend on the beast below.


          1 Reply Last reply
          0
          • W www Developerof NET

            Hi, I have a table which has a date column "OrderedDate" which can be null in db. Secondly i have a UI which has an frmdate and todate. While querying if the two textboxes are empty i need to show all the records( including the rows with null date). I have written the following query but it fetches the ros with non null values only.

            AND
            (
            isnull(OrderedDate,@frmdate) between isnull(@frmdate,OrderedDate) and isnull(@todate,OrderedDate)
            )

            I can`t figure out whats the problem. Kindly look into the matter if possible. Thank You.

            When you fail to plan, you are planning to fail.

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

            There are two things you can do in this case. 1. Either you can check the null value of @frmdate and @todate at programming level and then fire queries accordingly. 2. You can write the same condition in Stored procedure and fire query conditionally. But, by using single query it is not possible. Because when the @frmdate and @todate is null it will always consider those fields with value and ignore rows having null in OrderDate column, there are reason for such behavior. 1. Either between is evaluating while there is value in OrderDate column and ignoring row while it is null. Hope I will clear your problem.

            Jinal Desai

            1 Reply Last reply
            0
            • W www Developerof NET

              Hi, I have a table which has a date column "OrderedDate" which can be null in db. Secondly i have a UI which has an frmdate and todate. While querying if the two textboxes are empty i need to show all the records( including the rows with null date). I have written the following query but it fetches the ros with non null values only.

              AND
              (
              isnull(OrderedDate,@frmdate) between isnull(@frmdate,OrderedDate) and isnull(@todate,OrderedDate)
              )

              I can`t figure out whats the problem. Kindly look into the matter if possible. Thank You.

              When you fail to plan, you are planning to fail.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              I would take another approach. The Business Layer should not allow the UI layer to pass null values in; it should require proper DateTime values. The UI layer should then pass in something like System.Data.SqlTypes.SqlDateTime.MinValue or System.Data.SqlTypes.SqlDateTime.MaxValue as appropriate. You are parsing the TextBox values to DateTimes before passing them to the Business Layer aren't you? In fact, why aren't you using DateTimePickers? :confused:

              W 1 Reply Last reply
              0
              • W www Developerof NET

                Hi, I have a table which has a date column "OrderedDate" which can be null in db. Secondly i have a UI which has an frmdate and todate. While querying if the two textboxes are empty i need to show all the records( including the rows with null date). I have written the following query but it fetches the ros with non null values only.

                AND
                (
                isnull(OrderedDate,@frmdate) between isnull(@frmdate,OrderedDate) and isnull(@todate,OrderedDate)
                )

                I can`t figure out whats the problem. Kindly look into the matter if possible. Thank You.

                When you fail to plan, you are planning to fail.

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

                Following is conditional code that will work for your situation. declare @date1 as DateTime; declare @date2 as DateTime; If both the dates are specified the query goes as follow set @date1='1970-04-04 00:00:00.000'; set @date2='1978-07-07 00:00:00.000'; if @date1 is null or @date2 is null begin select * from tblemployee end else begin select * from tblemployee WHERE birthdate between @date1 and @date2 end If any of the field or both of the field is null then the query goes as follow set @date1=NULL; set @date2='1978-07-07 00:00:00.000'; if @date1 is null or @date2 is null begin select * from tblemployee end else begin select * from tblemployee WHERE birthdate between @date1 and @date2 end

                Jinal Desai

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  I would take another approach. The Business Layer should not allow the UI layer to pass null values in; it should require proper DateTime values. The UI layer should then pass in something like System.Data.SqlTypes.SqlDateTime.MinValue or System.Data.SqlTypes.SqlDateTime.MaxValue as appropriate. You are parsing the TextBox values to DateTimes before passing them to the Business Layer aren't you? In fact, why aren't you using DateTimePickers? :confused:

                  W Offline
                  W Offline
                  www Developerof NET
                  wrote on last edited by
                  #8

                  PIEBALDconsult wrote:

                  In fact, why aren't you using DateTimePickers?

                  Actually i am. I have a layered architecture and pass the datetime values as Nullable which allows me to send a null datetime to my SP. Anyways, Luc`s answer fits correctly for me.

                  AND
                  (
                  @frmdate IS NULL OR OrderedDate >= @frmdate
                  }
                  AND
                  (
                  @todate IS NULL OR OrderedDate <= @todate
                  )

                  Thanks again.

                  When you fail to plan, you are planning to fail.

                  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