Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Getting datepart to work for query


Getting datepart to work for query

Author
Message
shree dhavale
shree dhavale
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 40
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
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5695 Visits: 25280
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

Before posting a performance problem please read
Jeffrey Williams 3188
Jeffrey Williams 3188
SSCarpal Tunnel
SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)SSCarpal Tunnel (4.5K reputation)

Group: General Forum Members
Points: 4465 Visits: 9833
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
Problems are opportunities brilliantly disguised as insurmountable obstacles.

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

Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3264 Visits: 11771

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')


shree dhavale
shree dhavale
SSC-Enthusiastic
SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)SSC-Enthusiastic (147 reputation)

Group: General Forum Members
Points: 147 Visits: 40
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
Mark Eytcheson
Mark Eytcheson
Old Hand
Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)Old Hand (379 reputation)

Group: General Forum Members
Points: 379 Visits: 8672
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.
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24261 Visits: 37981
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Michael Valentine Jones
Michael Valentine Jones
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3264 Visits: 11771
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'.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search