GetDate() with time-ignore

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bassem
    Contributor
    • Dec 2008
    • 344

    GetDate() with time-ignore

    I'm trying to select all records inserted in a certain day, so comparing date only and ignore the time.
    I searched for a function but didn't found.
    I found this:
    Code:
    WHERE day([Date]) = day(GetDate()) AND month([Date]) = month(GetDate())
    AND year([Date]) = year(GetDate())
    Is to get the day, month and year then compare everyone of them.
    My code getting more bigger!
    Is there another easier way?

    >>Edit<<
    [Date] is a table in my database.
    >>/Edit<<

    Thanks,
    Bassem
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Read this...and this



    ---- CK

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      The first link doesn't work, the other one make it more complex to me I'll try more with it.

      Thanks,

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Here you go...


        --- CK

        Comment

        • zinkdude

          #5
          cto

          Here it is:

          select Cast(Cast(DateP art(m, GetDate()) as varchar) + '/' + Cast(DatePart(d , GetDate()) as varchar) + '/' + Cast(DatePart(y y, GetDate()) as varchar) as datetime)

          Comment

          • Bassem
            Contributor
            • Dec 2008
            • 344

            #6
            Will I still able to compare the datetime - the product of cast - to another one like a parameter?

            Comment

            • gpl
              New Member
              • Jul 2007
              • 152

              #7
              From the link in post #4, this is what you need
              Code:
              SELECT
              DATEADD(dd, DATEDIFF(dd,0,@MyDate), 0)
              as "Midnight for the given day"
              therefore your example now looks like
              Code:
              WHERE
              DATEADD(dd, DATEDIFF(dd,0,@MyDate), 0)=DATEADD(dd, DATEDIFF(dd,0,GetDate()), 0)

              Comment

              • Bassem
                Contributor
                • Dec 2008
                • 344

                #8
                Thank you gpl.

                Thanks ck9663 and zinkdude for your help.

                Comment

                Working...