Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Getting datepart to work for query Expand / Collapse
Author
Message
Posted Saturday, June 27, 2009 1:50 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, November 13, 2014 10:38 PM
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
Post #743164
Posted Saturday, June 27, 2009 3:14 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, November 6, 2014 1:00 PM
Points: 5,333, Visits: 25,277
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
Post #743176
Posted Saturday, June 27, 2009 7:45 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Friday, December 12, 2014 12:03 PM
Points: 4,363, Visits: 9,547
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 opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #743197
Posted Saturday, June 27, 2009 10:53 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 3,113, Visits: 11,543
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')

Post #743215
Posted Sunday, June 28, 2009 10:45 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, November 13, 2014 10:38 PM
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
Post #743348
Posted Monday, June 29, 2009 1:22 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 18, 2014 4:26 PM
Points: 377, Visits: 8,634
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.
Post #743908
Posted Monday, June 29, 2009 1:26 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:53 PM
Points: 20,865, Visits: 32,903
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.




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)
Post #743910
Posted Monday, June 29, 2009 9:23 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:56 PM
Points: 3,113, Visits: 11,543
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'.

Post #744135
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse