How can I add this new custom column to the output of my query?How can I remove duplicate rows?How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerHow to check if a column exists in a SQL Server table?How can I add a column to a Postgresql database that doesn't allow nulls?How can I get column names from a table in SQL Server?How can I do an UPDATE statement with JOIN in SQL?How to query MongoDB with “like”?What are the options for storing hierarchical data in a relational database?How to create SQL custom 4-4-5 Finanical Period date table
How can the US president give an order to a civilian?
My student in one course asks for paid tutoring in another course. Appropriate?
Predict the product from the reaction
I have found ports on my Samsung smart tv running a display service. What can I do with it?
Why things float in space, though there is always gravity of our star is present
In a list with unique pairs A, B, how can I sort them so that the last B is the first A in the next pair?
What is this airplane that sits in front of Barringer High School in Newark, NJ?
How did Frodo know where the Bree village was?
What is the highest power supply a Raspberry pi 3 B can handle without getting damaged?
How to write a nice frame challenge?
How do I find which software is doing an SSH connection?
Table the LCM of an increasing list
Bent arrow under a node
Why is it easier to balance a non-moving bike standing up than sitting down?
Make symbols atomic, without losing their type
How to modify a string without altering its text properties
I found a password with hashcat but it doesn't work
How is the idea of "girlfriend material" naturally expressed in Russian?
「捨ててしまう」why is there two て’s used here?
What is the maximum that Player 1 can win?
What is the most suitable position for a bishop here?
Can a character learn spells from someone else's spellbook and then sell it?
How do you transpose samples in cents?
Large-n limit of the distribution of the normalized sum of Cauchy random variables
How can I add this new custom column to the output of my query?
How can I remove duplicate rows?How can I prevent SQL injection in PHP?Add a column with a default value to an existing table in SQL ServerHow to check if a column exists in a SQL Server table?How can I add a column to a Postgresql database that doesn't allow nulls?How can I get column names from a table in SQL Server?How can I do an UPDATE statement with JOIN in SQL?How to query MongoDB with “like”?What are the options for storing hierarchical data in a relational database?How to create SQL custom 4-4-5 Finanical Period date table
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I am using SQL Server 2014 and I have the following T-SQL query:
SELECT
[Date],
(CASE
WHEN [Date] BETWEEN '2016-07-01' AND '2017-06-30' THEN 'FY 16-17'
WHEN [Date] BETWEEN '2017-07-01' AND '2018-06-30' THEN 'FY 17-18'
WHEN [Date] BETWEEN '2018-07-01' AND '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) AS [Period]
FROM
DateDimension
WHERE
[Date] BETWEEN '2016-07-01' AND '2019-06-30'
The output is as follows (extract):
Date Period
-----------------------
2016-07-01 FY 16-17
2016-07-02 FY 16-17
2016-07-03 FY 16-17
... ...
2017-07-01 FY 17-18
2017-07-02 FY 17-18
2017-07-03 FY 17-18
... ...
2018-07-01 FY 18-19
2018-07-02 FY 18-19
2018-07-03 FY 18-19
... ...
I want to add a new column to the output as follows:
Date Period Day
-------------------------------
2016-07-01 FY 16-17 D1
2016-07-02 FY 16-17 D2
2016-07-03 FY 16-17 D3
... ... ...
2017-07-01 FY 17-18 D1
2017-07-02 FY 17-18 D2
2017-07-03 FY 17-18 D3
... ... ...
2018-07-01 FY 18-19 D1
2018-07-02 FY 18-19 D2
2018-07-03 FY 18-19 D3
... ... ...
To note that D1
starts again at the beginning of each new financial year (that is,2016-07-01
, 2017-07-01
and 2018-07-01
).
How do I write the SQL code for this new column?
Additional note: D1
should be continuous till the end of each financial year. Example, from 2016-07-01
till 2017-06-30
, column Period
will show D1, D2, ..., D365)
sql sql-server tsql
add a comment |
I am using SQL Server 2014 and I have the following T-SQL query:
SELECT
[Date],
(CASE
WHEN [Date] BETWEEN '2016-07-01' AND '2017-06-30' THEN 'FY 16-17'
WHEN [Date] BETWEEN '2017-07-01' AND '2018-06-30' THEN 'FY 17-18'
WHEN [Date] BETWEEN '2018-07-01' AND '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) AS [Period]
FROM
DateDimension
WHERE
[Date] BETWEEN '2016-07-01' AND '2019-06-30'
The output is as follows (extract):
Date Period
-----------------------
2016-07-01 FY 16-17
2016-07-02 FY 16-17
2016-07-03 FY 16-17
... ...
2017-07-01 FY 17-18
2017-07-02 FY 17-18
2017-07-03 FY 17-18
... ...
2018-07-01 FY 18-19
2018-07-02 FY 18-19
2018-07-03 FY 18-19
... ...
I want to add a new column to the output as follows:
Date Period Day
-------------------------------
2016-07-01 FY 16-17 D1
2016-07-02 FY 16-17 D2
2016-07-03 FY 16-17 D3
... ... ...
2017-07-01 FY 17-18 D1
2017-07-02 FY 17-18 D2
2017-07-03 FY 17-18 D3
... ... ...
2018-07-01 FY 18-19 D1
2018-07-02 FY 18-19 D2
2018-07-03 FY 18-19 D3
... ... ...
To note that D1
starts again at the beginning of each new financial year (that is,2016-07-01
, 2017-07-01
and 2018-07-01
).
How do I write the SQL code for this new column?
Additional note: D1
should be continuous till the end of each financial year. Example, from 2016-07-01
till 2017-06-30
, column Period
will show D1, D2, ..., D365)
sql sql-server tsql
3
You are selecting from a table calledDateDimension
. I would expect this table to already contain date-related columns such as what financial year does the current date belong to and what day it is in that financial year...
– Zohar Peled
Mar 25 at 6:20
add a comment |
I am using SQL Server 2014 and I have the following T-SQL query:
SELECT
[Date],
(CASE
WHEN [Date] BETWEEN '2016-07-01' AND '2017-06-30' THEN 'FY 16-17'
WHEN [Date] BETWEEN '2017-07-01' AND '2018-06-30' THEN 'FY 17-18'
WHEN [Date] BETWEEN '2018-07-01' AND '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) AS [Period]
FROM
DateDimension
WHERE
[Date] BETWEEN '2016-07-01' AND '2019-06-30'
The output is as follows (extract):
Date Period
-----------------------
2016-07-01 FY 16-17
2016-07-02 FY 16-17
2016-07-03 FY 16-17
... ...
2017-07-01 FY 17-18
2017-07-02 FY 17-18
2017-07-03 FY 17-18
... ...
2018-07-01 FY 18-19
2018-07-02 FY 18-19
2018-07-03 FY 18-19
... ...
I want to add a new column to the output as follows:
Date Period Day
-------------------------------
2016-07-01 FY 16-17 D1
2016-07-02 FY 16-17 D2
2016-07-03 FY 16-17 D3
... ... ...
2017-07-01 FY 17-18 D1
2017-07-02 FY 17-18 D2
2017-07-03 FY 17-18 D3
... ... ...
2018-07-01 FY 18-19 D1
2018-07-02 FY 18-19 D2
2018-07-03 FY 18-19 D3
... ... ...
To note that D1
starts again at the beginning of each new financial year (that is,2016-07-01
, 2017-07-01
and 2018-07-01
).
How do I write the SQL code for this new column?
Additional note: D1
should be continuous till the end of each financial year. Example, from 2016-07-01
till 2017-06-30
, column Period
will show D1, D2, ..., D365)
sql sql-server tsql
I am using SQL Server 2014 and I have the following T-SQL query:
SELECT
[Date],
(CASE
WHEN [Date] BETWEEN '2016-07-01' AND '2017-06-30' THEN 'FY 16-17'
WHEN [Date] BETWEEN '2017-07-01' AND '2018-06-30' THEN 'FY 17-18'
WHEN [Date] BETWEEN '2018-07-01' AND '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) AS [Period]
FROM
DateDimension
WHERE
[Date] BETWEEN '2016-07-01' AND '2019-06-30'
The output is as follows (extract):
Date Period
-----------------------
2016-07-01 FY 16-17
2016-07-02 FY 16-17
2016-07-03 FY 16-17
... ...
2017-07-01 FY 17-18
2017-07-02 FY 17-18
2017-07-03 FY 17-18
... ...
2018-07-01 FY 18-19
2018-07-02 FY 18-19
2018-07-03 FY 18-19
... ...
I want to add a new column to the output as follows:
Date Period Day
-------------------------------
2016-07-01 FY 16-17 D1
2016-07-02 FY 16-17 D2
2016-07-03 FY 16-17 D3
... ... ...
2017-07-01 FY 17-18 D1
2017-07-02 FY 17-18 D2
2017-07-03 FY 17-18 D3
... ... ...
2018-07-01 FY 18-19 D1
2018-07-02 FY 18-19 D2
2018-07-03 FY 18-19 D3
... ... ...
To note that D1
starts again at the beginning of each new financial year (that is,2016-07-01
, 2017-07-01
and 2018-07-01
).
How do I write the SQL code for this new column?
Additional note: D1
should be continuous till the end of each financial year. Example, from 2016-07-01
till 2017-06-30
, column Period
will show D1, D2, ..., D365)
sql sql-server tsql
sql sql-server tsql
edited Mar 25 at 7:58
marc_s
593k13311361279
593k13311361279
asked Mar 25 at 6:14
user3115933user3115933
1,53352041
1,53352041
3
You are selecting from a table calledDateDimension
. I would expect this table to already contain date-related columns such as what financial year does the current date belong to and what day it is in that financial year...
– Zohar Peled
Mar 25 at 6:20
add a comment |
3
You are selecting from a table calledDateDimension
. I would expect this table to already contain date-related columns such as what financial year does the current date belong to and what day it is in that financial year...
– Zohar Peled
Mar 25 at 6:20
3
3
You are selecting from a table called
DateDimension
. I would expect this table to already contain date-related columns such as what financial year does the current date belong to and what day it is in that financial year...– Zohar Peled
Mar 25 at 6:20
You are selecting from a table called
DateDimension
. I would expect this table to already contain date-related columns such as what financial year does the current date belong to and what day it is in that financial year...– Zohar Peled
Mar 25 at 6:20
add a comment |
5 Answers
5
active
oldest
votes
use row_number()
with cte as
(
SELECT [Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
FROM DateDimension
)
select *,concat('D',row_number() over(partition by period order by date)) as DayNo
from cte
3
It seems you missed the point that you must start counting on July 1.
– Thorsten Kettner
Mar 25 at 6:24
3
Also, a single missing day in the table turnsrow_number()
into pumpkin.
– Roger Wolf
Mar 25 at 6:29
add a comment |
You could use DATEDIFF
to get the difference in days between the start of the financial year & [Date]
.
SELECT
[Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
, CASE WHEN [Date] < DATEFROMPARTS(DATEPART(Year, GETDATE()), 7, 1)
THEN CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]) - 1, 7, 1), [Date] + 1)))
ELSE CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]), 7, 1), [Date] + 1)))
END AS [Day]
FROM DateDimension
WHERE [Date] between '2016-07-01' and '2019-06-30'
This method also means that the dates could be in any order or even have some days missing & the Day
column should still be correct.
add a comment |
Here is an example how you can simplify your existing FY calculation, and get the day of financial year as well:
declare @date date = '20190702';
select year(dateadd(month, -6, @date)) as [FY],
datediff(day, datefromparts(year(dateadd(month, -6, @date)), 6, 30), @date) as [DOFY];
The hard-coded constants that designate the offset between the calendar and financial year can also be parameterised, if you need it.
add a comment |
You can use DATEDIFF
to calculate number of days in that Financial year. You just need an extra effort to get the Financial year first date for the DATE column.
DECLARE @DateDimension TABLE ([DATE] DATETIME)
INSERT INTO @DateDimension
SELECT '2019-03-25'
UNION ALL
SELECT '2018-12-06'
UNION ALL
SELECT '2018-05-15'
UNION ALL
SELECT '2017-11-22'
UNION ALL
SELECT '2019-07-06'
SELECT [DATE]
,'D'+CAST( DATEDIFF(DD, CASE WHEN MONTH([DATE]) BETWEEN 7 AND 12
THEN DATEFROMPARTS(YEAR([DATE]),07,01)
ELSE DATEFROMPARTS(YEAR([DATE])-1,07,01) END,[DATE])+1
AS VARCHAR(3)) AS DAY_IN_FY
FROM @DateDimension
Result:
+-------------------------+-----------+
| DATE | DAY_IN_FY |
+-------------------------+-----------+
| 2019-03-25 00:00:00.000 | D268 |
| 2018-12-06 00:00:00.000 | D159 |
| 2018-05-15 00:00:00.000 | D319 |
| 2017-11-22 00:00:00.000 | D145 |
| 2019-07-06 00:00:00.000 | D6 |
+-------------------------+-----------+
add a comment |
I want to point out that you can express the query as:
SELECT d.[Date], v.period,
CONCAT('D', ROW_NUMBER() OVER (PARTITION BY period ORDER BY date)) as [Day]
FROM DateDimension dd CROSS APPLY
(VALUES (RIGHT(DATENAME(year, d.[Date]), 2) + '-' +
RIGHT(DATENAME(year, DATEADD(year, 1, d.[Date])), 2)
)
) as v(period)
WHERE [Date] BETWEEN '2016-07-01' AND '2019-06-30';
The period
can also be defined as:
(VALUES (CONCAT(YEAR([Date] % 100, '-',
1 + YEAR([Date] % 100
)
)
) as v(period)
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%2f55332155%2fhow-can-i-add-this-new-custom-column-to-the-output-of-my-query%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
use row_number()
with cte as
(
SELECT [Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
FROM DateDimension
)
select *,concat('D',row_number() over(partition by period order by date)) as DayNo
from cte
3
It seems you missed the point that you must start counting on July 1.
– Thorsten Kettner
Mar 25 at 6:24
3
Also, a single missing day in the table turnsrow_number()
into pumpkin.
– Roger Wolf
Mar 25 at 6:29
add a comment |
use row_number()
with cte as
(
SELECT [Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
FROM DateDimension
)
select *,concat('D',row_number() over(partition by period order by date)) as DayNo
from cte
3
It seems you missed the point that you must start counting on July 1.
– Thorsten Kettner
Mar 25 at 6:24
3
Also, a single missing day in the table turnsrow_number()
into pumpkin.
– Roger Wolf
Mar 25 at 6:29
add a comment |
use row_number()
with cte as
(
SELECT [Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
FROM DateDimension
)
select *,concat('D',row_number() over(partition by period order by date)) as DayNo
from cte
use row_number()
with cte as
(
SELECT [Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
FROM DateDimension
)
select *,concat('D',row_number() over(partition by period order by date)) as DayNo
from cte
answered Mar 25 at 6:18
fa06fa06
22.3k41119
22.3k41119
3
It seems you missed the point that you must start counting on July 1.
– Thorsten Kettner
Mar 25 at 6:24
3
Also, a single missing day in the table turnsrow_number()
into pumpkin.
– Roger Wolf
Mar 25 at 6:29
add a comment |
3
It seems you missed the point that you must start counting on July 1.
– Thorsten Kettner
Mar 25 at 6:24
3
Also, a single missing day in the table turnsrow_number()
into pumpkin.
– Roger Wolf
Mar 25 at 6:29
3
3
It seems you missed the point that you must start counting on July 1.
– Thorsten Kettner
Mar 25 at 6:24
It seems you missed the point that you must start counting on July 1.
– Thorsten Kettner
Mar 25 at 6:24
3
3
Also, a single missing day in the table turns
row_number()
into pumpkin.– Roger Wolf
Mar 25 at 6:29
Also, a single missing day in the table turns
row_number()
into pumpkin.– Roger Wolf
Mar 25 at 6:29
add a comment |
You could use DATEDIFF
to get the difference in days between the start of the financial year & [Date]
.
SELECT
[Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
, CASE WHEN [Date] < DATEFROMPARTS(DATEPART(Year, GETDATE()), 7, 1)
THEN CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]) - 1, 7, 1), [Date] + 1)))
ELSE CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]), 7, 1), [Date] + 1)))
END AS [Day]
FROM DateDimension
WHERE [Date] between '2016-07-01' and '2019-06-30'
This method also means that the dates could be in any order or even have some days missing & the Day
column should still be correct.
add a comment |
You could use DATEDIFF
to get the difference in days between the start of the financial year & [Date]
.
SELECT
[Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
, CASE WHEN [Date] < DATEFROMPARTS(DATEPART(Year, GETDATE()), 7, 1)
THEN CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]) - 1, 7, 1), [Date] + 1)))
ELSE CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]), 7, 1), [Date] + 1)))
END AS [Day]
FROM DateDimension
WHERE [Date] between '2016-07-01' and '2019-06-30'
This method also means that the dates could be in any order or even have some days missing & the Day
column should still be correct.
add a comment |
You could use DATEDIFF
to get the difference in days between the start of the financial year & [Date]
.
SELECT
[Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
, CASE WHEN [Date] < DATEFROMPARTS(DATEPART(Year, GETDATE()), 7, 1)
THEN CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]) - 1, 7, 1), [Date] + 1)))
ELSE CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]), 7, 1), [Date] + 1)))
END AS [Day]
FROM DateDimension
WHERE [Date] between '2016-07-01' and '2019-06-30'
This method also means that the dates could be in any order or even have some days missing & the Day
column should still be correct.
You could use DATEDIFF
to get the difference in days between the start of the financial year & [Date]
.
SELECT
[Date]
,(CASE WHEN [Date] between '2016-07-01' and '2017-06-30' THEN 'FY 16-17'
WHEN [Date] between '2017-07-01' and '2018-06-30' THEN 'FY 17-18'
WHEN [Date] between '2018-07-01' and '2019-06-30' THEN 'FY 18-19'
ELSE 'Not Stated'
END) as [Period]
, CASE WHEN [Date] < DATEFROMPARTS(DATEPART(Year, GETDATE()), 7, 1)
THEN CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]) - 1, 7, 1), [Date] + 1)))
ELSE CONCAT('D', (DATEDIFF(DAY, DATEFROMPARTS(DATEPART(Year, [Date]), 7, 1), [Date] + 1)))
END AS [Day]
FROM DateDimension
WHERE [Date] between '2016-07-01' and '2019-06-30'
This method also means that the dates could be in any order or even have some days missing & the Day
column should still be correct.
answered Mar 25 at 6:32
ItsPeteItsPete
1,2971628
1,2971628
add a comment |
add a comment |
Here is an example how you can simplify your existing FY calculation, and get the day of financial year as well:
declare @date date = '20190702';
select year(dateadd(month, -6, @date)) as [FY],
datediff(day, datefromparts(year(dateadd(month, -6, @date)), 6, 30), @date) as [DOFY];
The hard-coded constants that designate the offset between the calendar and financial year can also be parameterised, if you need it.
add a comment |
Here is an example how you can simplify your existing FY calculation, and get the day of financial year as well:
declare @date date = '20190702';
select year(dateadd(month, -6, @date)) as [FY],
datediff(day, datefromparts(year(dateadd(month, -6, @date)), 6, 30), @date) as [DOFY];
The hard-coded constants that designate the offset between the calendar and financial year can also be parameterised, if you need it.
add a comment |
Here is an example how you can simplify your existing FY calculation, and get the day of financial year as well:
declare @date date = '20190702';
select year(dateadd(month, -6, @date)) as [FY],
datediff(day, datefromparts(year(dateadd(month, -6, @date)), 6, 30), @date) as [DOFY];
The hard-coded constants that designate the offset between the calendar and financial year can also be parameterised, if you need it.
Here is an example how you can simplify your existing FY calculation, and get the day of financial year as well:
declare @date date = '20190702';
select year(dateadd(month, -6, @date)) as [FY],
datediff(day, datefromparts(year(dateadd(month, -6, @date)), 6, 30), @date) as [DOFY];
The hard-coded constants that designate the offset between the calendar and financial year can also be parameterised, if you need it.
answered Mar 25 at 6:50
Roger WolfRoger Wolf
4,8581918
4,8581918
add a comment |
add a comment |
You can use DATEDIFF
to calculate number of days in that Financial year. You just need an extra effort to get the Financial year first date for the DATE column.
DECLARE @DateDimension TABLE ([DATE] DATETIME)
INSERT INTO @DateDimension
SELECT '2019-03-25'
UNION ALL
SELECT '2018-12-06'
UNION ALL
SELECT '2018-05-15'
UNION ALL
SELECT '2017-11-22'
UNION ALL
SELECT '2019-07-06'
SELECT [DATE]
,'D'+CAST( DATEDIFF(DD, CASE WHEN MONTH([DATE]) BETWEEN 7 AND 12
THEN DATEFROMPARTS(YEAR([DATE]),07,01)
ELSE DATEFROMPARTS(YEAR([DATE])-1,07,01) END,[DATE])+1
AS VARCHAR(3)) AS DAY_IN_FY
FROM @DateDimension
Result:
+-------------------------+-----------+
| DATE | DAY_IN_FY |
+-------------------------+-----------+
| 2019-03-25 00:00:00.000 | D268 |
| 2018-12-06 00:00:00.000 | D159 |
| 2018-05-15 00:00:00.000 | D319 |
| 2017-11-22 00:00:00.000 | D145 |
| 2019-07-06 00:00:00.000 | D6 |
+-------------------------+-----------+
add a comment |
You can use DATEDIFF
to calculate number of days in that Financial year. You just need an extra effort to get the Financial year first date for the DATE column.
DECLARE @DateDimension TABLE ([DATE] DATETIME)
INSERT INTO @DateDimension
SELECT '2019-03-25'
UNION ALL
SELECT '2018-12-06'
UNION ALL
SELECT '2018-05-15'
UNION ALL
SELECT '2017-11-22'
UNION ALL
SELECT '2019-07-06'
SELECT [DATE]
,'D'+CAST( DATEDIFF(DD, CASE WHEN MONTH([DATE]) BETWEEN 7 AND 12
THEN DATEFROMPARTS(YEAR([DATE]),07,01)
ELSE DATEFROMPARTS(YEAR([DATE])-1,07,01) END,[DATE])+1
AS VARCHAR(3)) AS DAY_IN_FY
FROM @DateDimension
Result:
+-------------------------+-----------+
| DATE | DAY_IN_FY |
+-------------------------+-----------+
| 2019-03-25 00:00:00.000 | D268 |
| 2018-12-06 00:00:00.000 | D159 |
| 2018-05-15 00:00:00.000 | D319 |
| 2017-11-22 00:00:00.000 | D145 |
| 2019-07-06 00:00:00.000 | D6 |
+-------------------------+-----------+
add a comment |
You can use DATEDIFF
to calculate number of days in that Financial year. You just need an extra effort to get the Financial year first date for the DATE column.
DECLARE @DateDimension TABLE ([DATE] DATETIME)
INSERT INTO @DateDimension
SELECT '2019-03-25'
UNION ALL
SELECT '2018-12-06'
UNION ALL
SELECT '2018-05-15'
UNION ALL
SELECT '2017-11-22'
UNION ALL
SELECT '2019-07-06'
SELECT [DATE]
,'D'+CAST( DATEDIFF(DD, CASE WHEN MONTH([DATE]) BETWEEN 7 AND 12
THEN DATEFROMPARTS(YEAR([DATE]),07,01)
ELSE DATEFROMPARTS(YEAR([DATE])-1,07,01) END,[DATE])+1
AS VARCHAR(3)) AS DAY_IN_FY
FROM @DateDimension
Result:
+-------------------------+-----------+
| DATE | DAY_IN_FY |
+-------------------------+-----------+
| 2019-03-25 00:00:00.000 | D268 |
| 2018-12-06 00:00:00.000 | D159 |
| 2018-05-15 00:00:00.000 | D319 |
| 2017-11-22 00:00:00.000 | D145 |
| 2019-07-06 00:00:00.000 | D6 |
+-------------------------+-----------+
You can use DATEDIFF
to calculate number of days in that Financial year. You just need an extra effort to get the Financial year first date for the DATE column.
DECLARE @DateDimension TABLE ([DATE] DATETIME)
INSERT INTO @DateDimension
SELECT '2019-03-25'
UNION ALL
SELECT '2018-12-06'
UNION ALL
SELECT '2018-05-15'
UNION ALL
SELECT '2017-11-22'
UNION ALL
SELECT '2019-07-06'
SELECT [DATE]
,'D'+CAST( DATEDIFF(DD, CASE WHEN MONTH([DATE]) BETWEEN 7 AND 12
THEN DATEFROMPARTS(YEAR([DATE]),07,01)
ELSE DATEFROMPARTS(YEAR([DATE])-1,07,01) END,[DATE])+1
AS VARCHAR(3)) AS DAY_IN_FY
FROM @DateDimension
Result:
+-------------------------+-----------+
| DATE | DAY_IN_FY |
+-------------------------+-----------+
| 2019-03-25 00:00:00.000 | D268 |
| 2018-12-06 00:00:00.000 | D159 |
| 2018-05-15 00:00:00.000 | D319 |
| 2017-11-22 00:00:00.000 | D145 |
| 2019-07-06 00:00:00.000 | D6 |
+-------------------------+-----------+
answered Mar 25 at 7:09
Shakeer MirzaShakeer Mirza
4,40021236
4,40021236
add a comment |
add a comment |
I want to point out that you can express the query as:
SELECT d.[Date], v.period,
CONCAT('D', ROW_NUMBER() OVER (PARTITION BY period ORDER BY date)) as [Day]
FROM DateDimension dd CROSS APPLY
(VALUES (RIGHT(DATENAME(year, d.[Date]), 2) + '-' +
RIGHT(DATENAME(year, DATEADD(year, 1, d.[Date])), 2)
)
) as v(period)
WHERE [Date] BETWEEN '2016-07-01' AND '2019-06-30';
The period
can also be defined as:
(VALUES (CONCAT(YEAR([Date] % 100, '-',
1 + YEAR([Date] % 100
)
)
) as v(period)
add a comment |
I want to point out that you can express the query as:
SELECT d.[Date], v.period,
CONCAT('D', ROW_NUMBER() OVER (PARTITION BY period ORDER BY date)) as [Day]
FROM DateDimension dd CROSS APPLY
(VALUES (RIGHT(DATENAME(year, d.[Date]), 2) + '-' +
RIGHT(DATENAME(year, DATEADD(year, 1, d.[Date])), 2)
)
) as v(period)
WHERE [Date] BETWEEN '2016-07-01' AND '2019-06-30';
The period
can also be defined as:
(VALUES (CONCAT(YEAR([Date] % 100, '-',
1 + YEAR([Date] % 100
)
)
) as v(period)
add a comment |
I want to point out that you can express the query as:
SELECT d.[Date], v.period,
CONCAT('D', ROW_NUMBER() OVER (PARTITION BY period ORDER BY date)) as [Day]
FROM DateDimension dd CROSS APPLY
(VALUES (RIGHT(DATENAME(year, d.[Date]), 2) + '-' +
RIGHT(DATENAME(year, DATEADD(year, 1, d.[Date])), 2)
)
) as v(period)
WHERE [Date] BETWEEN '2016-07-01' AND '2019-06-30';
The period
can also be defined as:
(VALUES (CONCAT(YEAR([Date] % 100, '-',
1 + YEAR([Date] % 100
)
)
) as v(period)
I want to point out that you can express the query as:
SELECT d.[Date], v.period,
CONCAT('D', ROW_NUMBER() OVER (PARTITION BY period ORDER BY date)) as [Day]
FROM DateDimension dd CROSS APPLY
(VALUES (RIGHT(DATENAME(year, d.[Date]), 2) + '-' +
RIGHT(DATENAME(year, DATEADD(year, 1, d.[Date])), 2)
)
) as v(period)
WHERE [Date] BETWEEN '2016-07-01' AND '2019-06-30';
The period
can also be defined as:
(VALUES (CONCAT(YEAR([Date] % 100, '-',
1 + YEAR([Date] % 100
)
)
) as v(period)
answered Mar 25 at 11:21
Gordon LinoffGordon Linoff
822k38333441
822k38333441
add a comment |
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%2f55332155%2fhow-can-i-add-this-new-custom-column-to-the-output-of-my-query%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
3
You are selecting from a table called
DateDimension
. I would expect this table to already contain date-related columns such as what financial year does the current date belong to and what day it is in that financial year...– Zohar Peled
Mar 25 at 6:20