Select query to retrieve data based on current date and time

  • I have problem in using SQL Server 2008. The new data types time and date are unknown to me. I cannot able to retrieve the datas from the database based on current time. I have two columns in the database table namely Date and Time whose data types are respectively date and time(0).

    Now what i needed is to retrieve the data from the database based on date and time. As time changes it must retrieve the current date and time datas which are already available in the database.

    Plz, help me in this regard. Any help from you will highly be appreciated.

  • There are likely other ways to achieve this, but you can do the following:

    Select * from TableName T

    where T.Date = Convert(date,getdate())

    and T.Time = CONVERT(time,getdate())

    You get records where the date and time match the current date and time EXACTLY.

    --------------------------------------------------------------------------
    When you realize you've dug yourself into a hole....Step 1...stop digging.

  • Thank u Sir for your immense help but when i execute the query i did not get any result. I want to ask one more question, whether the query must match the exact time i.e in hours, minute and seconds.

    Actually can we do some kind of casting to match only the exact hours only, so that it will check for the hours from the Time column and get the data.

    Thank you once again.

  • Table definition, sample data and desired result please.

    Using the time data type as Chaz showed requires matches to the nanosecond.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Actually, i have a table name district_data where the fields are district_id,Temperature, Rainfall,Wind, Date and Time.

    district_id Temperature Rainfall Wind Date Time

    1 17.5 5.0 8.0 2011-03-09 10:00:00

    1 18.5 6.0 7.0 2011-03-09 11:00:00

    It means the table contains the datas for every 1 hour interval. i must fetch the current time datas. I must have the value same for 10:00:00-10:59:59 i.e Temperature=17.5, when the time changes to 11:00:00 i must have Temperature=18.5, upto 11:59:59.

    i must consider only the hour part. Plz, help me. I am trying it from 1 week, but still no sign of any success.

    Plz, give me some suggestion so that i can continue.....

    Thank you Sir, for giving me your time....

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

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