MS Access SQL - How to pull last 13 months data from the first of the first month?
I tried the below code to pull last 13 months of data. But it is not pulling from the 1st of the month. How can I modify it to pull data from the first day of the month?
For e.g. If I am running it today (11/13/18), I want data from 11/1/17 to today.
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATE());
sql ms-access ms-access-2016
add a comment |
I tried the below code to pull last 13 months of data. But it is not pulling from the 1st of the month. How can I modify it to pull data from the first day of the month?
For e.g. If I am running it today (11/13/18), I want data from 11/1/17 to today.
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATE());
sql ms-access ms-access-2016
add a comment |
I tried the below code to pull last 13 months of data. But it is not pulling from the 1st of the month. How can I modify it to pull data from the first day of the month?
For e.g. If I am running it today (11/13/18), I want data from 11/1/17 to today.
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATE());
sql ms-access ms-access-2016
I tried the below code to pull last 13 months of data. But it is not pulling from the 1st of the month. How can I modify it to pull data from the first day of the month?
For e.g. If I am running it today (11/13/18), I want data from 11/1/17 to today.
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATE());
sql ms-access ms-access-2016
sql ms-access ms-access-2016
edited Nov 13 '18 at 17:23
Gordon Linoff
767k35300402
767k35300402
asked Nov 13 '18 at 17:23
SKaySKay
51
51
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You need to change the day to 1 as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("d", -(DAY(Date())-1),DATEADD("m", -13, DATE());
Or by using DATESERIAL()
function which return a DATE
from parts as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATESERIAL(YEAR(Date()), MONTH(DATE()), 1))
It will get the YEAR
part from the date which is 2018
in your case, then get the MONTH
part which is 11 and 1
which is the day and concatenate them to return a DATE
as 2018-11-1
Great job, now you made an exact copy...
– forpas
Nov 13 '18 at 17:51
Oh yeah that's what you think
– Sami
Nov 13 '18 at 17:52
Of course it is
– forpas
Nov 13 '18 at 17:53
Thank you @Sami. I used Dateseriel function.
– SKay
Nov 13 '18 at 22:30
add a comment |
This:
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, Dateserial(year(date()), month(date()),1));
uses
Dateserial(year(date()), month(date()),1)
which gives you the 1st of the current month
so you subtract from this the 13 months.
Thank you @forpas, it worked!
– SKay
Nov 13 '18 at 22:28
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%2f53286453%2fms-access-sql-how-to-pull-last-13-months-data-from-the-first-of-the-first-mont%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to change the day to 1 as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("d", -(DAY(Date())-1),DATEADD("m", -13, DATE());
Or by using DATESERIAL()
function which return a DATE
from parts as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATESERIAL(YEAR(Date()), MONTH(DATE()), 1))
It will get the YEAR
part from the date which is 2018
in your case, then get the MONTH
part which is 11 and 1
which is the day and concatenate them to return a DATE
as 2018-11-1
Great job, now you made an exact copy...
– forpas
Nov 13 '18 at 17:51
Oh yeah that's what you think
– Sami
Nov 13 '18 at 17:52
Of course it is
– forpas
Nov 13 '18 at 17:53
Thank you @Sami. I used Dateseriel function.
– SKay
Nov 13 '18 at 22:30
add a comment |
You need to change the day to 1 as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("d", -(DAY(Date())-1),DATEADD("m", -13, DATE());
Or by using DATESERIAL()
function which return a DATE
from parts as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATESERIAL(YEAR(Date()), MONTH(DATE()), 1))
It will get the YEAR
part from the date which is 2018
in your case, then get the MONTH
part which is 11 and 1
which is the day and concatenate them to return a DATE
as 2018-11-1
Great job, now you made an exact copy...
– forpas
Nov 13 '18 at 17:51
Oh yeah that's what you think
– Sami
Nov 13 '18 at 17:52
Of course it is
– forpas
Nov 13 '18 at 17:53
Thank you @Sami. I used Dateseriel function.
– SKay
Nov 13 '18 at 22:30
add a comment |
You need to change the day to 1 as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("d", -(DAY(Date())-1),DATEADD("m", -13, DATE());
Or by using DATESERIAL()
function which return a DATE
from parts as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATESERIAL(YEAR(Date()), MONTH(DATE()), 1))
It will get the YEAR
part from the date which is 2018
in your case, then get the MONTH
part which is 11 and 1
which is the day and concatenate them to return a DATE
as 2018-11-1
You need to change the day to 1 as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("d", -(DAY(Date())-1),DATEADD("m", -13, DATE());
Or by using DATESERIAL()
function which return a DATE
from parts as
SELECT *
FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, DATESERIAL(YEAR(Date()), MONTH(DATE()), 1))
It will get the YEAR
part from the date which is 2018
in your case, then get the MONTH
part which is 11 and 1
which is the day and concatenate them to return a DATE
as 2018-11-1
edited Nov 13 '18 at 17:49
answered Nov 13 '18 at 17:35
SamiSami
8,72331241
8,72331241
Great job, now you made an exact copy...
– forpas
Nov 13 '18 at 17:51
Oh yeah that's what you think
– Sami
Nov 13 '18 at 17:52
Of course it is
– forpas
Nov 13 '18 at 17:53
Thank you @Sami. I used Dateseriel function.
– SKay
Nov 13 '18 at 22:30
add a comment |
Great job, now you made an exact copy...
– forpas
Nov 13 '18 at 17:51
Oh yeah that's what you think
– Sami
Nov 13 '18 at 17:52
Of course it is
– forpas
Nov 13 '18 at 17:53
Thank you @Sami. I used Dateseriel function.
– SKay
Nov 13 '18 at 22:30
Great job, now you made an exact copy...
– forpas
Nov 13 '18 at 17:51
Great job, now you made an exact copy...
– forpas
Nov 13 '18 at 17:51
Oh yeah that's what you think
– Sami
Nov 13 '18 at 17:52
Oh yeah that's what you think
– Sami
Nov 13 '18 at 17:52
Of course it is
– forpas
Nov 13 '18 at 17:53
Of course it is
– forpas
Nov 13 '18 at 17:53
Thank you @Sami. I used Dateseriel function.
– SKay
Nov 13 '18 at 22:30
Thank you @Sami. I used Dateseriel function.
– SKay
Nov 13 '18 at 22:30
add a comment |
This:
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, Dateserial(year(date()), month(date()),1));
uses
Dateserial(year(date()), month(date()),1)
which gives you the 1st of the current month
so you subtract from this the 13 months.
Thank you @forpas, it worked!
– SKay
Nov 13 '18 at 22:28
add a comment |
This:
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, Dateserial(year(date()), month(date()),1));
uses
Dateserial(year(date()), month(date()),1)
which gives you the 1st of the current month
so you subtract from this the 13 months.
Thank you @forpas, it worked!
– SKay
Nov 13 '18 at 22:28
add a comment |
This:
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, Dateserial(year(date()), month(date()),1));
uses
Dateserial(year(date()), month(date()),1)
which gives you the 1st of the current month
so you subtract from this the 13 months.
This:
SELECT * FROM MyTable T
WHERE T.[Date Joined] >= DATEADD("m", -13, Dateserial(year(date()), month(date()),1));
uses
Dateserial(year(date()), month(date()),1)
which gives you the 1st of the current month
so you subtract from this the 13 months.
answered Nov 13 '18 at 17:40
forpasforpas
11k3423
11k3423
Thank you @forpas, it worked!
– SKay
Nov 13 '18 at 22:28
add a comment |
Thank you @forpas, it worked!
– SKay
Nov 13 '18 at 22:28
Thank you @forpas, it worked!
– SKay
Nov 13 '18 at 22:28
Thank you @forpas, it worked!
– SKay
Nov 13 '18 at 22:28
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%2f53286453%2fms-access-sql-how-to-pull-last-13-months-data-from-the-first-of-the-first-mont%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