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;








0















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)










share|improve this question



















  • 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

















0















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)










share|improve this question



















  • 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













0












0








0


1






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)










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












5 Answers
5






active

oldest

votes


















-1














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





share|improve this answer


















  • 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 turns row_number() into pumpkin.

    – Roger Wolf
    Mar 25 at 6:29


















0














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.






share|improve this answer






























    0














    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.






    share|improve this answer






























      0














      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 |
      +-------------------------+-----------+





      share|improve this answer






























        0














        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)





        share|improve this answer























          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
          );



          );













          draft saved

          draft discarded


















          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









          -1














          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





          share|improve this answer


















          • 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 turns row_number() into pumpkin.

            – Roger Wolf
            Mar 25 at 6:29















          -1














          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





          share|improve this answer


















          • 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 turns row_number() into pumpkin.

            – Roger Wolf
            Mar 25 at 6:29













          -1












          -1








          -1







          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





          share|improve this answer













          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 turns row_number() into pumpkin.

            – Roger Wolf
            Mar 25 at 6:29












          • 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 turns row_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













          0














          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.






          share|improve this answer



























            0














            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.






            share|improve this answer

























              0












              0








              0







              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.






              share|improve this answer













              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.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Mar 25 at 6:32









              ItsPeteItsPete

              1,2971628




              1,2971628





















                  0














                  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.






                  share|improve this answer



























                    0














                    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.






                    share|improve this answer

























                      0












                      0








                      0







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 25 at 6:50









                      Roger WolfRoger Wolf

                      4,8581918




                      4,8581918





















                          0














                          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 |
                          +-------------------------+-----------+





                          share|improve this answer



























                            0














                            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 |
                            +-------------------------+-----------+





                            share|improve this answer

























                              0












                              0








                              0







                              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 |
                              +-------------------------+-----------+





                              share|improve this answer













                              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 |
                              +-------------------------+-----------+






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 25 at 7:09









                              Shakeer MirzaShakeer Mirza

                              4,40021236




                              4,40021236





















                                  0














                                  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)





                                  share|improve this answer



























                                    0














                                    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)





                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      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)





                                      share|improve this answer













                                      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)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 25 at 11:21









                                      Gordon LinoffGordon Linoff

                                      822k38333441




                                      822k38333441



























                                          draft saved

                                          draft discarded
















































                                          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.




                                          draft saved


                                          draft discarded














                                          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





















































                                          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







                                          Popular posts from this blog

                                          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                                          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript