July 14, 2013 at 2:34 pm
HI all I have been asked to generate reports from my c# package I have created every Monday at 0700hrs, the previous week till Monday 0700hrs.
Monday 0700 > 0000
Tues 24hrs
Wed 24hrs
Thurs 24hrs
Fri 24hrs
Sat 24hrs
Sun 24hrs
Mon 0000 > 0700
1 weeks worth of data 0700hrs Monday to Monday
can anybody help me make this possible?
I am using this but it needs amending for time and I am not sure how!
select * from dbo.DocketTB where Docket_EngFinish between (getdate()-7) and getdate()
Docket_EngFinish is a DateTime Datatype
Many thanks for help and advice
Jay
July 14, 2013 at 2:53 pm
If you're running only on Mondays, these formulas might help you. If the day can vary, you might need an additional operation.
select DATEADD( hh, 7, DATEADD( dd, DATEDIFF( dd, 0, DATEADD(dd, -7, getdate())), 0)),
DATEADD( hh, 7, DATEADD( dd, DATEDIFF( dd, 0, getdate()), 0))
July 18, 2013 at 3:07 am
That works but am struggling to apply that to my query
select * from dbo.DocketTB where Docket_EngFinish DATEADD( hh, 7, DATEADD( dd, DATEDIFF( dd, 0, DATEADD(dd, -7, getdate())), 0)),
DATEADD( hh, 7, DATEADD( dd, DATEDIFF( dd, 0, getdate()), 0))
how do I add the where Docket_EngFinish column
Jay
July 18, 2013 at 3:42 am
-- step-by-step workings: calculate 7am of monday last week and 7am of monday this week
SELECT *
FROM (SELECT DTToday = GETDATE()) d
CROSS APPLY (SELECT NoOfDaysSinceMonday = DATEDIFF(DD,0,DTToday)%7) x1
CROSS APPLY (SELECT DTMondayLastWeek = DATEADD(DD,-(NoOfDaysSinceMonday+7),DTToday)) x2
CROSS APPLY (SELECT MondayLastWeek = DATEADD(HH,7,CAST(CAST(DTMondayLastWeek AS DATE) AS DATETIME))) x3
CROSS APPLY (SELECT MondayThisWeek = DATEADD(DD,7,x3.MondayLastWeek)) x4
-- solution:
SELECT *
FROM dbo.DocketTB
CROSS APPLY (
SELECT MondayLastWeek = DATEADD(hh,7,CAST(CAST(DATEADD(DD,-((DATEDIFF(DD,0,GETDATE())%7)+7),GETDATE()) AS DATE) AS DATETIME))
) x1
WHERE Docket_EngFinish BETWEEN x1.MondayLastWeek AND DATEADD(DD,7,x1.MondayLastWeek)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 18, 2013 at 4:31 am
Hi Chris from the step by step I see the correct dates, but on the solution it returns no results. Is there now need for the lastweek thisweek between value in this?
As always Thanks
July 18, 2013 at 4:59 am
Sorry, must learn to scroll down 🙁
J
July 18, 2013 at 5:00 am
hmmm still no result even tho I no there is data in there for the time period
July 18, 2013 at 5:04 am
jerome.morris (7/18/2013)
hmmm still no result even tho I no there is data in there for the time period
Post your query, Jerome.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 18, 2013 at 5:08 am
SELECT *
FROM dbo.DocketTB
CROSS APPLY (
SELECT MondayLastWeek = DATEADD(hh,7,CAST(CAST(DATEADD(DD,-((DATEDIFF(DD,0,GETDATE())%7)+7),GETDATE()) AS DATE) AS DATETIME))
) x1
WHERE Docket_EngFinish BETWEEN x1.MondayLastWeek AND DATEADD(DD,7,x1.MondayLastWeek)
returns no results
SELECT *
FROM (SELECT DTToday = GETDATE()) d
CROSS APPLY (SELECT NoOfDaysSinceMonday = DATEDIFF(DD,0,DTToday)%7) x1
CROSS APPLY (SELECT DTMondayLastWeek = DATEADD(DD,-(NoOfDaysSinceMonday+7),DTToday)) x2
CROSS APPLY (SELECT MondayLastWeek = DATEADD(HH,7,CAST(CAST(DTMondayLastWeek AS DATE) AS DATETIME))) x3
CROSS APPLY (SELECT MondayThisWeek = DATEADD(DD,7,x3.MondayLastWeek)) x4
returns
2013-07-18 12:03:36.55332013-07-08 12:03:36.5532013-07-08 07:00:00.0002013-07-15 07:00:00.000
July 18, 2013 at 5:11 am
USE [SLADB]
GO
/****** Object: Table [dbo].[DocketTB] Script Date: 07/18/2013 12:11:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DocketTB](
[Docket_Id] [int] IDENTITY(1,1) NOT NULL,
[Docket_Number] [int] NULL,
[Docket_Machine] [nchar](30) NULL,
[Docket_Status] [nchar](10) NULL,
[Docket_EngineerName] [nchar](50) NULL,
[Docket_Category] [nchar](50) NULL,
[Docket_SubCategory] [nchar](50) NULL,
[Duration] [int] NULL,
[Module] [nchar](100) NULL,
[Section] [nchar](100) NULL,
[Waittime] [int] NULL,
[Operator_Name] [nchar](100) NULL,
[Monitor_Time] [int] NULL,
[spare8] [nchar](100) NULL,
[Docket_EngStart] [datetime] NULL,
[Docket_EngFinish] [datetime] NULL,
[Docket_DateRaised] [datetime] NULL,
[Docket_Date] [datetime] NULL,
[Contract] [bit] NULL,
[Weekend] [bit] NULL,
[ReasonReq] [bit] NULL,
[Reason] [text] NULL,
CONSTRAINT [PK_DocketTB] PRIMARY KEY CLUSTERED
(
[Docket_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
July 18, 2013 at 5:38 am
What's returned from this, Jerome?
SELECT *
FROM dbo.DocketTB
WHERE Docket_EngFinish BETWEEN '20130708' AND '20131508'
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 18, 2013 at 6:38 am
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
July 18, 2013 at 6:43 am
jerome.morris (7/18/2013)
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Oops sorry my mistake:
SELECT *
FROM dbo.DocketTB
WHERE Docket_EngFinish BETWEEN '20130708' AND '20130715'
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 18, 2013 at 6:50 am
Hi Chris that returns data within them dates.
Thanks
July 19, 2013 at 1:24 am
jerome.morris (7/18/2013)
Hi Chris that returns data within them dates.Thanks
Time to put this one to bed. Can you post up the results of this batch please?
DECLARE @RangeStart DATETIME, @RangeEnd DATETIME
SELECT
@RangeStart = x1.MondayLastWeek,
@RangeEnd = DATEADD(DD,7,x1.MondayLastWeek)
FROM (
SELECT MondayLastWeek = DATEADD(hh,7,CAST(CAST(DATEADD(DD,-((DATEDIFF(DD,0,GETDATE())%7)+7),GETDATE()) AS DATE) AS DATETIME))
) x1
SELECT @RangeStart, @RangeEnd
SELECT *
FROM dbo.DocketTB
WHERE Docket_EngFinish BETWEEN @RangeStart AND @RangeEnd
SELECT *
FROM dbo.DocketTB
CROSS APPLY (
SELECT MondayLastWeek = DATEADD(hh,7,CAST(CAST(DATEADD(DD,-((DATEDIFF(DD,0,GETDATE())%7)+7),GETDATE()) AS DATE) AS DATETIME))
) x1
WHERE Docket_EngFinish BETWEEN x1.MondayLastWeek AND DATEADD(DD,7,x1.MondayLastWeek)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 15 posts - 1 through 15 (of 49 total)
You must be logged in to reply to this topic. Login to reply