Query for specific records each new day

  • I would like to query for 5 new records everyday. For example, the records at 4am in the morning. I would like to do so without having to enter the date part for each new day. Is there a way of doing so?

    Thanks SQL2DAY

  • Please will you post your table structure and some sample data and expected results. It's difficult to visualise your problem without this.

    John

  • John--

    Lets see if I can recall from memory--

    Select Data_ID, DateTime and Batt_Vltg from tableName where DateTime = Max(DateTime) which would be the current day and additionally where the time = 4am in the morning. This would give all the battery voltages at 4 am for the current day.

    Data_ID DateTime Batt_Vltg

    1 12/20/2007 04:00 AM 12.2

    2 12/20/2007 04:00 AM 12.6

    3 12/20/2007 04:00 AM 13.0

    SQL2DAY

  • You desire query will look like.

    SELECT

    Data_ID

    , DateTime

    , Batt_Vltg

    FROM

    tableName

    WHERE

    DateTime = CAST((CONVERT(VARCHAR(10),GETDATE(),103) + ' 04:00AM') AS DATETIME)

    Current where clause give you your required result.

    cheers

  • SELECT Data_ID, DateTime, Batt_Vltg

    FROM

    tableName

    WHERE

    DateTime = DATEADD(hh,4,DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0))

    Doing conversions to Varchar to trim the time off a date is inefficient and CPU intensive.

    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
  • Thank You!

    I will try and let you know how it does....

    Thanks Again..... SQL2DAY

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

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