How to extract records that have a sent date after a different time of day for each day of the week?Help with hard sql query to update based on daily totals to summary tableTransact-SQL: Detect Date boundaries across multiple rowsComplex SQL queryNormalizing a tableSQL - Get Minutes between 2 datetime but only for WorkdaysBreaking down totals by Agent then by dayTrying to find gaps between times in two separate columns and separate rowsSQL - Generate bi-weekly end date on custom start dateSQL how to count census points occurring between date recordsChecking to see if today is in the scheduled day utilizing a binary mapping table in SQL
Do equal angles necessarily mean a polygon is regular?
How do I find and plot the intersection of these three surfaces?
How would a order of Monks that renounce their names communicate effectively?
How can I create ribbons like these in Microsoft word 2010?
A player is constantly pestering me about rules, what do I do as a DM?
Why isn’t the tax system continuous rather than bracketed?
Why does the A-4 Skyhawk sit nose-up when on ground?
Do we or do we not observe (measure) superpositions all the time?
Word Wall of Whimsical Wordy Whatchamacallits
Dold-Kan correspondence in the category of symmetric spectra
Set vertical spacing between two particular items
Why does the numerical solution of an ODE move away from an unstable equilibrium?
Averting Real Women Don’t Wear Dresses
Is there any set of 2-6 notes that doesn't have a chord name?
Should I include salary information on my CV?
Wilcoxon signed rank test – critical value for n>50
Children's short story about material that accelerates away from gravity
Analog is Obtuse!
How exactly is a normal force exerted, at the molecular level?
Short story with brother-sister conjoined twins as protagonists?
How can I check type T is among parameter pack Ts... in C++?
Could Sauron have read Tom Bombadil's mind if Tom had held the Palantir?
What is the line crossing the Pacific Ocean that is shown on maps?
Why is Madam Hooch not a professor?
How to extract records that have a sent date after a different time of day for each day of the week?
Help with hard sql query to update based on daily totals to summary tableTransact-SQL: Detect Date boundaries across multiple rowsComplex SQL queryNormalizing a tableSQL - Get Minutes between 2 datetime but only for WorkdaysBreaking down totals by Agent then by dayTrying to find gaps between times in two separate columns and separate rowsSQL - Generate bi-weekly end date on custom start dateSQL how to count census points occurring between date recordsChecking to see if today is in the scheduled day utilizing a binary mapping table in SQL
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a table holding email data in particular the date and time they were sent. I need to extract the records that were sent after a given time each day (ie after close of business). The problem is that the close of business time each day can be different. Currently we just use after 6pm, but some businesses may finish early on Friday compared to the rest of the week.
I can do individual single queries along the lines of:
SELECT DataExtractTableID FROM DataExtractTable WHERE (Box = 'Sent Items') AND
(Owner = @EmailAddress) AND
(DateSent BETWEEN @StartDate AND @EndDate)
AND (CAST(DateSent as time) >= CAST(@MondayCloseOfBusiness as time));
Then repeat this for each day of the week and then dump all the DataExtractTableIDs into a single temporary table to get my total list of DataExtractTableIDs that relate to emails set after the individual times each day.
This all seems a bit long winded and I wondered if there is a cleverer solution that can be done in a single query or SP?
tsql
add a comment |
I have a table holding email data in particular the date and time they were sent. I need to extract the records that were sent after a given time each day (ie after close of business). The problem is that the close of business time each day can be different. Currently we just use after 6pm, but some businesses may finish early on Friday compared to the rest of the week.
I can do individual single queries along the lines of:
SELECT DataExtractTableID FROM DataExtractTable WHERE (Box = 'Sent Items') AND
(Owner = @EmailAddress) AND
(DateSent BETWEEN @StartDate AND @EndDate)
AND (CAST(DateSent as time) >= CAST(@MondayCloseOfBusiness as time));
Then repeat this for each day of the week and then dump all the DataExtractTableIDs into a single temporary table to get my total list of DataExtractTableIDs that relate to emails set after the individual times each day.
This all seems a bit long winded and I wondered if there is a cleverer solution that can be done in a single query or SP?
tsql
add a comment |
I have a table holding email data in particular the date and time they were sent. I need to extract the records that were sent after a given time each day (ie after close of business). The problem is that the close of business time each day can be different. Currently we just use after 6pm, but some businesses may finish early on Friday compared to the rest of the week.
I can do individual single queries along the lines of:
SELECT DataExtractTableID FROM DataExtractTable WHERE (Box = 'Sent Items') AND
(Owner = @EmailAddress) AND
(DateSent BETWEEN @StartDate AND @EndDate)
AND (CAST(DateSent as time) >= CAST(@MondayCloseOfBusiness as time));
Then repeat this for each day of the week and then dump all the DataExtractTableIDs into a single temporary table to get my total list of DataExtractTableIDs that relate to emails set after the individual times each day.
This all seems a bit long winded and I wondered if there is a cleverer solution that can be done in a single query or SP?
tsql
I have a table holding email data in particular the date and time they were sent. I need to extract the records that were sent after a given time each day (ie after close of business). The problem is that the close of business time each day can be different. Currently we just use after 6pm, but some businesses may finish early on Friday compared to the rest of the week.
I can do individual single queries along the lines of:
SELECT DataExtractTableID FROM DataExtractTable WHERE (Box = 'Sent Items') AND
(Owner = @EmailAddress) AND
(DateSent BETWEEN @StartDate AND @EndDate)
AND (CAST(DateSent as time) >= CAST(@MondayCloseOfBusiness as time));
Then repeat this for each day of the week and then dump all the DataExtractTableIDs into a single temporary table to get my total list of DataExtractTableIDs that relate to emails set after the individual times each day.
This all seems a bit long winded and I wondered if there is a cleverer solution that can be done in a single query or SP?
tsql
tsql
asked Mar 25 at 11:40
SivSiv
298 bronze badges
298 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There are two options here.
- Add a table (it can also be a temporary table / table variable) for coupling weekday and business hours, then join to this table on weekday:
SET DATEFIRST 7;
DECLARE @WorkingHours AS TABLE
(
WeekDay int,
OpenTime Time,
CloseTime Time
);
INSERT INTO @WorkingHours(WeekDay, OpenTime, CloseTime) VALUES
(1, '08:00:00', '18:00:00'),
(2, '08:30:00', '18:30:00'),
(3, '09:00:00', '19:00:00'),
(4, '08:00:00', '18:00:00'),
(5, '08:00:00', '18:00:00'),
(6, '08:00:00', '12:00:00');
SELECT DataExtractTableID
FROM DataExtractTable
JOIN @WorkingHours AS WH
ON DATEPART(WEEKDAY, DataExtractTable.DateSent) = WH.WeekDay
WHERE (Box = 'Sent Items')
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= WH.CloseTime;
- Use a case expression instead:
SET DATEFIRST 7;
DECLARE @SundayClosingTime Time = '16:00:00',
@MondayClosingTime Time = '12:00:00',
@RegularClosingTime Time = '18:00:00';
SELECT DataExtractTableID
FROM DataExtractTable
WHERE Box = 'Sent Items'
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= CASE DATEPART(WEEKDAY, DateSent)
WHEN 1 THEN @SundayClosingTime
WHEN 2 THEN @MondayClosingTime
ELSE @RegularClosingTime
END;
Please note that DatePart output for weekday depends on regional settings - specifically, the first day of the week which can be configured by SET DATEFIRST.
Zohar, thanks for your answer, I will give that a try I haven't used case statements as per your second option so am interested to understand that method.
– Siv
Mar 26 at 8:27
I've edited my answer to add a link to case official documentation. Basically, it's an expression returning a scalar value based on condition.
– Zohar Peled
Mar 26 at 8:29
Zohar, thanks for your help, I had to remove the ELSE statement in my version as where I am using this I am only interested in Monday to Friday so I need to ignore the weekend. I have tested the results and it works like a charm and much less onerous than the way I had been doing it. Thanks for the link. Thanks again.
– Siv
Mar 26 at 14:16
Glad to help :-)
– Zohar Peled
Mar 26 at 14:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55336995%2fhow-to-extract-records-that-have-a-sent-date-after-a-different-time-of-day-for-e%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are two options here.
- Add a table (it can also be a temporary table / table variable) for coupling weekday and business hours, then join to this table on weekday:
SET DATEFIRST 7;
DECLARE @WorkingHours AS TABLE
(
WeekDay int,
OpenTime Time,
CloseTime Time
);
INSERT INTO @WorkingHours(WeekDay, OpenTime, CloseTime) VALUES
(1, '08:00:00', '18:00:00'),
(2, '08:30:00', '18:30:00'),
(3, '09:00:00', '19:00:00'),
(4, '08:00:00', '18:00:00'),
(5, '08:00:00', '18:00:00'),
(6, '08:00:00', '12:00:00');
SELECT DataExtractTableID
FROM DataExtractTable
JOIN @WorkingHours AS WH
ON DATEPART(WEEKDAY, DataExtractTable.DateSent) = WH.WeekDay
WHERE (Box = 'Sent Items')
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= WH.CloseTime;
- Use a case expression instead:
SET DATEFIRST 7;
DECLARE @SundayClosingTime Time = '16:00:00',
@MondayClosingTime Time = '12:00:00',
@RegularClosingTime Time = '18:00:00';
SELECT DataExtractTableID
FROM DataExtractTable
WHERE Box = 'Sent Items'
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= CASE DATEPART(WEEKDAY, DateSent)
WHEN 1 THEN @SundayClosingTime
WHEN 2 THEN @MondayClosingTime
ELSE @RegularClosingTime
END;
Please note that DatePart output for weekday depends on regional settings - specifically, the first day of the week which can be configured by SET DATEFIRST.
Zohar, thanks for your answer, I will give that a try I haven't used case statements as per your second option so am interested to understand that method.
– Siv
Mar 26 at 8:27
I've edited my answer to add a link to case official documentation. Basically, it's an expression returning a scalar value based on condition.
– Zohar Peled
Mar 26 at 8:29
Zohar, thanks for your help, I had to remove the ELSE statement in my version as where I am using this I am only interested in Monday to Friday so I need to ignore the weekend. I have tested the results and it works like a charm and much less onerous than the way I had been doing it. Thanks for the link. Thanks again.
– Siv
Mar 26 at 14:16
Glad to help :-)
– Zohar Peled
Mar 26 at 14:19
add a comment |
There are two options here.
- Add a table (it can also be a temporary table / table variable) for coupling weekday and business hours, then join to this table on weekday:
SET DATEFIRST 7;
DECLARE @WorkingHours AS TABLE
(
WeekDay int,
OpenTime Time,
CloseTime Time
);
INSERT INTO @WorkingHours(WeekDay, OpenTime, CloseTime) VALUES
(1, '08:00:00', '18:00:00'),
(2, '08:30:00', '18:30:00'),
(3, '09:00:00', '19:00:00'),
(4, '08:00:00', '18:00:00'),
(5, '08:00:00', '18:00:00'),
(6, '08:00:00', '12:00:00');
SELECT DataExtractTableID
FROM DataExtractTable
JOIN @WorkingHours AS WH
ON DATEPART(WEEKDAY, DataExtractTable.DateSent) = WH.WeekDay
WHERE (Box = 'Sent Items')
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= WH.CloseTime;
- Use a case expression instead:
SET DATEFIRST 7;
DECLARE @SundayClosingTime Time = '16:00:00',
@MondayClosingTime Time = '12:00:00',
@RegularClosingTime Time = '18:00:00';
SELECT DataExtractTableID
FROM DataExtractTable
WHERE Box = 'Sent Items'
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= CASE DATEPART(WEEKDAY, DateSent)
WHEN 1 THEN @SundayClosingTime
WHEN 2 THEN @MondayClosingTime
ELSE @RegularClosingTime
END;
Please note that DatePart output for weekday depends on regional settings - specifically, the first day of the week which can be configured by SET DATEFIRST.
Zohar, thanks for your answer, I will give that a try I haven't used case statements as per your second option so am interested to understand that method.
– Siv
Mar 26 at 8:27
I've edited my answer to add a link to case official documentation. Basically, it's an expression returning a scalar value based on condition.
– Zohar Peled
Mar 26 at 8:29
Zohar, thanks for your help, I had to remove the ELSE statement in my version as where I am using this I am only interested in Monday to Friday so I need to ignore the weekend. I have tested the results and it works like a charm and much less onerous than the way I had been doing it. Thanks for the link. Thanks again.
– Siv
Mar 26 at 14:16
Glad to help :-)
– Zohar Peled
Mar 26 at 14:19
add a comment |
There are two options here.
- Add a table (it can also be a temporary table / table variable) for coupling weekday and business hours, then join to this table on weekday:
SET DATEFIRST 7;
DECLARE @WorkingHours AS TABLE
(
WeekDay int,
OpenTime Time,
CloseTime Time
);
INSERT INTO @WorkingHours(WeekDay, OpenTime, CloseTime) VALUES
(1, '08:00:00', '18:00:00'),
(2, '08:30:00', '18:30:00'),
(3, '09:00:00', '19:00:00'),
(4, '08:00:00', '18:00:00'),
(5, '08:00:00', '18:00:00'),
(6, '08:00:00', '12:00:00');
SELECT DataExtractTableID
FROM DataExtractTable
JOIN @WorkingHours AS WH
ON DATEPART(WEEKDAY, DataExtractTable.DateSent) = WH.WeekDay
WHERE (Box = 'Sent Items')
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= WH.CloseTime;
- Use a case expression instead:
SET DATEFIRST 7;
DECLARE @SundayClosingTime Time = '16:00:00',
@MondayClosingTime Time = '12:00:00',
@RegularClosingTime Time = '18:00:00';
SELECT DataExtractTableID
FROM DataExtractTable
WHERE Box = 'Sent Items'
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= CASE DATEPART(WEEKDAY, DateSent)
WHEN 1 THEN @SundayClosingTime
WHEN 2 THEN @MondayClosingTime
ELSE @RegularClosingTime
END;
Please note that DatePart output for weekday depends on regional settings - specifically, the first day of the week which can be configured by SET DATEFIRST.
There are two options here.
- Add a table (it can also be a temporary table / table variable) for coupling weekday and business hours, then join to this table on weekday:
SET DATEFIRST 7;
DECLARE @WorkingHours AS TABLE
(
WeekDay int,
OpenTime Time,
CloseTime Time
);
INSERT INTO @WorkingHours(WeekDay, OpenTime, CloseTime) VALUES
(1, '08:00:00', '18:00:00'),
(2, '08:30:00', '18:30:00'),
(3, '09:00:00', '19:00:00'),
(4, '08:00:00', '18:00:00'),
(5, '08:00:00', '18:00:00'),
(6, '08:00:00', '12:00:00');
SELECT DataExtractTableID
FROM DataExtractTable
JOIN @WorkingHours AS WH
ON DATEPART(WEEKDAY, DataExtractTable.DateSent) = WH.WeekDay
WHERE (Box = 'Sent Items')
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= WH.CloseTime;
- Use a case expression instead:
SET DATEFIRST 7;
DECLARE @SundayClosingTime Time = '16:00:00',
@MondayClosingTime Time = '12:00:00',
@RegularClosingTime Time = '18:00:00';
SELECT DataExtractTableID
FROM DataExtractTable
WHERE Box = 'Sent Items'
AND Owner = @EmailAddress
AND DateSent BETWEEN @StartDate AND @EndDate
AND CAST(DateSent as time) >= CASE DATEPART(WEEKDAY, DateSent)
WHEN 1 THEN @SundayClosingTime
WHEN 2 THEN @MondayClosingTime
ELSE @RegularClosingTime
END;
Please note that DatePart output for weekday depends on regional settings - specifically, the first day of the week which can be configured by SET DATEFIRST.
edited Mar 26 at 8:29
answered Mar 25 at 12:41
Zohar PeledZohar Peled
59.2k7 gold badges35 silver badges77 bronze badges
59.2k7 gold badges35 silver badges77 bronze badges
Zohar, thanks for your answer, I will give that a try I haven't used case statements as per your second option so am interested to understand that method.
– Siv
Mar 26 at 8:27
I've edited my answer to add a link to case official documentation. Basically, it's an expression returning a scalar value based on condition.
– Zohar Peled
Mar 26 at 8:29
Zohar, thanks for your help, I had to remove the ELSE statement in my version as where I am using this I am only interested in Monday to Friday so I need to ignore the weekend. I have tested the results and it works like a charm and much less onerous than the way I had been doing it. Thanks for the link. Thanks again.
– Siv
Mar 26 at 14:16
Glad to help :-)
– Zohar Peled
Mar 26 at 14:19
add a comment |
Zohar, thanks for your answer, I will give that a try I haven't used case statements as per your second option so am interested to understand that method.
– Siv
Mar 26 at 8:27
I've edited my answer to add a link to case official documentation. Basically, it's an expression returning a scalar value based on condition.
– Zohar Peled
Mar 26 at 8:29
Zohar, thanks for your help, I had to remove the ELSE statement in my version as where I am using this I am only interested in Monday to Friday so I need to ignore the weekend. I have tested the results and it works like a charm and much less onerous than the way I had been doing it. Thanks for the link. Thanks again.
– Siv
Mar 26 at 14:16
Glad to help :-)
– Zohar Peled
Mar 26 at 14:19
Zohar, thanks for your answer, I will give that a try I haven't used case statements as per your second option so am interested to understand that method.
– Siv
Mar 26 at 8:27
Zohar, thanks for your answer, I will give that a try I haven't used case statements as per your second option so am interested to understand that method.
– Siv
Mar 26 at 8:27
I've edited my answer to add a link to case official documentation. Basically, it's an expression returning a scalar value based on condition.
– Zohar Peled
Mar 26 at 8:29
I've edited my answer to add a link to case official documentation. Basically, it's an expression returning a scalar value based on condition.
– Zohar Peled
Mar 26 at 8:29
Zohar, thanks for your help, I had to remove the ELSE statement in my version as where I am using this I am only interested in Monday to Friday so I need to ignore the weekend. I have tested the results and it works like a charm and much less onerous than the way I had been doing it. Thanks for the link. Thanks again.
– Siv
Mar 26 at 14:16
Zohar, thanks for your help, I had to remove the ELSE statement in my version as where I am using this I am only interested in Monday to Friday so I need to ignore the weekend. I have tested the results and it works like a charm and much less onerous than the way I had been doing it. Thanks for the link. Thanks again.
– Siv
Mar 26 at 14:16
Glad to help :-)
– Zohar Peled
Mar 26 at 14:19
Glad to help :-)
– Zohar Peled
Mar 26 at 14:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55336995%2fhow-to-extract-records-that-have-a-sent-date-after-a-different-time-of-day-for-e%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown