getting values for current day using getDate() functon

  • I have a table that has the following field

    [LOGDATETIMESTAMP] [datetime] NULL ,

    which basically contains the date and time that the row was inserted

    I want to return all of values where the datetimeStamp is = to the current day.

    Is this possible using the getDate Function. I dont want to hard code the day as this will be used by a stored procedure

  • mpeters (9/23/2008)


    I have a table that has the following field

    [LOGDATETIMESTAMP] [datetime] NULL ,

    which basically contains the date and time that the row was inserted

    I want to return all of values where the datetimeStamp is = to the current day.

    Is this possible using the getDate Function. I dont want to hard code the day as this will be used by a stored procedure

    Where LOGDATETIMESTAMP>=dateadd(day,datediff(day,0,getdate()),0) and LOGDATETIMESTAMP<dateadd(day,datediff(day,0,getdate()),1)


    Madhivanan

    Failing to plan is Planning to fail

  • Hi mpeters

    Here's the theory:

    Get the current datetime using getdate()

    Strip off the time component, which gives today

    Add a day to the result, which gives tomorrow with no time component:

    [font="Courier New"]DECLARE @Today DATETIME, @Tomorrow DATETIME

    SET @Today = DATEADD(dd,DATEDIFF(dd, 0, GETDATE()), 0) -- '2008-09-23 00:00:00.000'

    SET @Tomorrow = DATEADD(dd,1,@Today) -- '2008-09-24 00:00:00.000'

    SELECT @Today, @Tomorrow -- Check the results

    SELECT *

    FROM yourtable

    WHERE [LOGDATETIMESTAMP] >= @Today AND [LOGDATETIMESTAMP] < @Tomorrow[/font]

    Cheers

    ChrisM

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Awesome, thank for all the help guys, thought there might have been a function that made it a bit easier

Viewing 4 posts - 1 through 3 (of 3 total)

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