Datetime Checks

  • I have a table with a column as dateentered with datatype varchar(10). I want to check this column with getdate(). Now since getdate() return date with time. and if i do cast(dateenetered as datetime) - this return only date with tiem as 00:00:00. How is it possibel to check only the date part ex if dateentered values are

    2009-01-07 00:00:00.000

    2009-01-07 00:00:00.000

    and if select getdate() returns 2009-01-08 14:42:15.613

    i need to check only 2009-01-07 with 2009-01-08 ignoring the time. ANy help on this will be greatly appreciated. TIA

  • There are several ways to strip the time out, here is one:

    select cast(floor(cast(getdate() as Float)) as datetime)

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • RBarryYoung (1/8/2009)


    There are several ways to strip the time out, here is one:

    select cast(floor(cast(getdate() as Float)) as datetime)

    I had always used

    select cast(convert(char(10), getdate(), 101) as datetime)

    but this way is more slick.

  • The "DATEDIFF" method is supposed to be the fastest, but I can never remember it off the top of my head.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Figured it out:

    Select DATEADD(dd,DATEDIFF(dd,0, getdate()), 0)

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • If the following formula results in zero, the data was entered today, regardless of time...

    SELECT DATEDIFF(dd,datecolumn,GETDATE())

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply