Getting datepart to work for query

  • Hi,

    I have a report where I need to see all current activity in a mfg shop from 4:30 am of yesterday to 4:29 am of today. I cant seem get the datepart to work for my time part of the date. Could someone please help me with the syntax for -

    Where startdate >= yesterday 4:30 am and startdate <=today 4:29 am
    There is also a report I need to generate for Monday 4:30a.m to Saturday 4:30 am every week.
    Thanks for all the help in advance!
    Shree

  • shree dhavale

    It would help those who will be attempting to help you if you provided the table structure, sample data and the T-SQL statement that you have attempted to utilize. All this per the article whose link is in my signature block.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Here are a few things for you to look at:

    Select dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()), 0))

    ,dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()) - 1, 0))

    Now, with the above you could do something like:

    Select {columns}

    From YourTable

    Where YourDate >= dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()), 0))

    And YourDate < dateadd(minute, 990, dateadd(day, datediff(day, 0, getdate()) - 1, 0))

    Jeffrey Williams
    “We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”

    ― Charles R. Swindoll

    How to post questions to get better answers faster
    Managing Transaction Logs

  • select

    *

    from

    MyTable

    where

    -- Greater than or equal to yesterday at 04:30

    MyDate >= dateadd(day,datediff(day,0,getdate())-1,'04:30')and

    -- Less than today at 04:30

    MyDate < dateadd(day,datediff(day,0,getdate()) ,'04:30')

  • Thanks so much for helping out. I will use the syntax posted in the solution.

    Here is the part that I had needed help with in my where clause...

    (where (timesheet.wcstart between @start_date and @end_date and timesheet.wcfinish is null))

    INSERT INTO #timesheet

    SELECT emp.firstname + ' ' + emp.surname, mo.monumber, inv.customer_part_num, inv.description, t.opno, routing.description,workcenter.name,

    Case When timesheet.wcstart < @start_date then @start_date

    Else timesheet.wcstart

    End,

    timesheet.wcfinish,

    ISNULL(workcenter.machine,'X'),

    '',

    TSNo

    FROM mfg_timesheet t

    Left outer join cubs..mfg_mo_routing routing On

    routing.companyid = timesheet.companyid and

    routing.mo_key = timesheet.mo_key and

    routing.opno = timesheet.opno

    Left outer join cubs..mfg_workcenter workcenter On

    workcenter.wcno = timesheet.wcno and

    workcenter.companyid = timesheet.companyid,

    mfg_mo mo, inv_item inv,mfg_employee emp,slsord sls

    where (timesheet.wcstart between @start_date and @end_date and timesheet.wcfinish is null) and

    mo.mo_key = timesheet.mo_key and

    emp.employeeid = timesheet.employeeid and

    mo.companyid = inv.company_id and

    mo.stocknumber = inv.stock_number and

    timesheet.companyid = 1 and

    workcenter.machine IN ('W') And

    TSNo NOT IN (SELECT TsNo FROM mfg_timesheetAdj) and

    sls.company_id = mo.companyid and

    sls.slsord_number = mo.slsord_number

  • I usually create two datasets:

    1. StartDate

    SELECT CAST((CONVERT(varchar(10), GETDATE()-9, 101)) AS DATETIME) AS StartDate

    2. EndDate

    SELECT CAST((CONVERT(varchar(10), GETDATE()-2, 101) + ' 23:59:59') AS DATETIME) AS EndDate

    Then set the parameters @StartDate and @EndDate equal to the values of these two datasets.

  • Mark Eytcheson (6/29/2009)


    I usually create two datasets:

    1. StartDate

    SELECT CAST((CONVERT(varchar(10), GETDATE()-9, 101)) AS DATETIME) AS StartDate

    2. EndDate

    SELECT CAST((CONVERT(varchar(10), GETDATE()-2, 101) + ' 23:59:59') AS DATETIME) AS EndDate

    Then set the parameters @StartDate and @EndDate equal to the values of these two datasets.

    SELECT CAST((CONVERT(varchar(10), GETDATE()-2, 101) + ' 23:59:59') AS DATETIME) AS EndDate --> This will miss any records with a datetime value > [Date] 23:59:59.000 and [Date + 1] 00:00:00.000.

  • Queries for a date range should normally be in this form:

    where MyDatetimeColumn >= @StartDatetime and MyDatetimeColumn < @EndDatetime

    In other words greater than or equal to the first point in time that we know that we want, and less than the first point in time that we don't want.

    There is never any guarantee that the precision of time intervals will stay the same in future releases, so it is unrealistic to code end time as "2009-06-29 23:59:59.997", even if this is the last possible moment that we want to select with the current version of SQL Server. Just code it as less than '2009-06-30'.

Viewing 8 posts - 1 through 7 (of 7 total)

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