Split date range into separate records












4















Today I have a problem with splitting a date range so it becomes two separate records.



Here is an example



----------------------------------------------------------------
| Record id | date_from | date_to |
----------------------------------------------------------------
| A | 2017-02-03 08:00:00.000 | 2017-02-04 17:00:00.000|
----------------------------------------------------------------


For the result I want this



----------------------------------------------------------------
| Record id | date_from | date_to |
----------------------------------------------------------------
| A | 2017-02-03 08:00:00.000 | 2017-02-03 23:59:59.000|
----------------------------------------------------------------
| A | 2017-02-04 00:00:00.000 | 2017-02-04 17:00:00.000|
----------------------------------------------------------------


Is there anything that can enlighten me with this problem? Thank you very much for your advice.



PS: this is dynamic and there is no limit in duration. If the "from" day is 2017-02-02 at 17:00, and the "to" day is 2017-02-04 17:00 then there will be three records, one of which is date range from 2017-02-03 00:00:00 until 2017-02-03 23:59:59.



For midnight, I guess it is based on the date time default. For the real problem, I have this table:



enter image description here



As you can see, from the detail for seeding mailbox databse, the range is like one day as assumption, so I guess it is more like that example :)



PS: I am using SQL Server 2014.










share|improve this question





























    4















    Today I have a problem with splitting a date range so it becomes two separate records.



    Here is an example



    ----------------------------------------------------------------
    | Record id | date_from | date_to |
    ----------------------------------------------------------------
    | A | 2017-02-03 08:00:00.000 | 2017-02-04 17:00:00.000|
    ----------------------------------------------------------------


    For the result I want this



    ----------------------------------------------------------------
    | Record id | date_from | date_to |
    ----------------------------------------------------------------
    | A | 2017-02-03 08:00:00.000 | 2017-02-03 23:59:59.000|
    ----------------------------------------------------------------
    | A | 2017-02-04 00:00:00.000 | 2017-02-04 17:00:00.000|
    ----------------------------------------------------------------


    Is there anything that can enlighten me with this problem? Thank you very much for your advice.



    PS: this is dynamic and there is no limit in duration. If the "from" day is 2017-02-02 at 17:00, and the "to" day is 2017-02-04 17:00 then there will be three records, one of which is date range from 2017-02-03 00:00:00 until 2017-02-03 23:59:59.



    For midnight, I guess it is based on the date time default. For the real problem, I have this table:



    enter image description here



    As you can see, from the detail for seeding mailbox databse, the range is like one day as assumption, so I guess it is more like that example :)



    PS: I am using SQL Server 2014.










    share|improve this question



























      4












      4








      4








      Today I have a problem with splitting a date range so it becomes two separate records.



      Here is an example



      ----------------------------------------------------------------
      | Record id | date_from | date_to |
      ----------------------------------------------------------------
      | A | 2017-02-03 08:00:00.000 | 2017-02-04 17:00:00.000|
      ----------------------------------------------------------------


      For the result I want this



      ----------------------------------------------------------------
      | Record id | date_from | date_to |
      ----------------------------------------------------------------
      | A | 2017-02-03 08:00:00.000 | 2017-02-03 23:59:59.000|
      ----------------------------------------------------------------
      | A | 2017-02-04 00:00:00.000 | 2017-02-04 17:00:00.000|
      ----------------------------------------------------------------


      Is there anything that can enlighten me with this problem? Thank you very much for your advice.



      PS: this is dynamic and there is no limit in duration. If the "from" day is 2017-02-02 at 17:00, and the "to" day is 2017-02-04 17:00 then there will be three records, one of which is date range from 2017-02-03 00:00:00 until 2017-02-03 23:59:59.



      For midnight, I guess it is based on the date time default. For the real problem, I have this table:



      enter image description here



      As you can see, from the detail for seeding mailbox databse, the range is like one day as assumption, so I guess it is more like that example :)



      PS: I am using SQL Server 2014.










      share|improve this question
















      Today I have a problem with splitting a date range so it becomes two separate records.



      Here is an example



      ----------------------------------------------------------------
      | Record id | date_from | date_to |
      ----------------------------------------------------------------
      | A | 2017-02-03 08:00:00.000 | 2017-02-04 17:00:00.000|
      ----------------------------------------------------------------


      For the result I want this



      ----------------------------------------------------------------
      | Record id | date_from | date_to |
      ----------------------------------------------------------------
      | A | 2017-02-03 08:00:00.000 | 2017-02-03 23:59:59.000|
      ----------------------------------------------------------------
      | A | 2017-02-04 00:00:00.000 | 2017-02-04 17:00:00.000|
      ----------------------------------------------------------------


      Is there anything that can enlighten me with this problem? Thank you very much for your advice.



      PS: this is dynamic and there is no limit in duration. If the "from" day is 2017-02-02 at 17:00, and the "to" day is 2017-02-04 17:00 then there will be three records, one of which is date range from 2017-02-03 00:00:00 until 2017-02-03 23:59:59.



      For midnight, I guess it is based on the date time default. For the real problem, I have this table:



      enter image description here



      As you can see, from the detail for seeding mailbox databse, the range is like one day as assumption, so I guess it is more like that example :)



      PS: I am using SQL Server 2014.







      sql-server sql-server-2014






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 3 '17 at 8:48









      Michael Green

      15k83162




      15k83162










      asked Apr 3 '17 at 5:41









      Faris FajarFaris Fajar

      212




      212






















          2 Answers
          2






          active

          oldest

          votes


















          4














          One way to do it is to use a table of numbers and CROSS APPLY.



          Sample data



          DECLARE @T TABLE (RecordID int, date_from datetime2(0), date_to datetime2(0));

          INSERT INTO @T (RecordID, date_from, date_to) VALUES
          (1, '2017-02-03 08:00:00' , '2017-02-04 17:00:00'),
          (2, '2017-02-05 08:00:00' , '2017-02-05 17:00:00'),
          (3, '2017-02-06 08:00:00' , '2017-02-10 17:00:00');


          Query



          In this example I generated a table of 10 numbers on the fly (CTE_Numbers). In production I have a permanent table with 100K numbers.



          WITH 
          CTE_Numbers1(n)
          AS
          (
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
          SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
          )
          ,CTE_Numbers
          AS
          (
          SELECT ROW_NUMBER() OVER (ORDER BY n) AS Number
          FROM CTE_Numbers1
          )
          SELECT
          T.RecordID
          ,CASE WHEN CA.Number0 = 0
          THEN date_from
          ELSE DATEADD(day, CA.Number0, CAST(T.date_from as date))
          END AS new_date_from
          ,CASE WHEN CA.Number0 = DATEDIFF(day, T.date_from, T.date_to)
          THEN date_to
          ELSE DATEADD(day, CA.Number0 + 1, CAST(T.date_from as date))
          END AS new_date_to
          FROM
          @T AS T
          CROSS APPLY
          (
          SELECT CTE_Numbers.Number - 1 AS Number0
          FROM CTE_Numbers
          WHERE CTE_Numbers.Number <= DATEDIFF(day, T.date_from, T.date_to) + 1
          ) AS CA
          ORDER BY
          RecordID
          ,new_date_from
          ;


          Result



          +----------+---------------------+---------------------+
          | RecordID | new_date_from | new_date_to |
          +----------+---------------------+---------------------+
          | 1 | 2017-02-03 08:00:00 | 2017-02-04 00:00:00 |
          | 1 | 2017-02-04 00:00:00 | 2017-02-04 17:00:00 |
          | 2 | 2017-02-05 08:00:00 | 2017-02-05 17:00:00 |
          | 3 | 2017-02-06 08:00:00 | 2017-02-07 00:00:00 |
          | 3 | 2017-02-07 00:00:00 | 2017-02-08 00:00:00 |
          | 3 | 2017-02-08 00:00:00 | 2017-02-09 00:00:00 |
          | 3 | 2017-02-09 00:00:00 | 2017-02-10 00:00:00 |
          | 3 | 2017-02-10 00:00:00 | 2017-02-10 17:00:00 |
          +----------+---------------------+---------------------+





          share|improve this answer


























          • Great effort, but I think its still not matching the requirements from OP, the date from is greater than date to; Also, date should be same in single row.

            – MarmiK
            Apr 3 '17 at 11:48













          • @MarmiK, the result of my query is [closed; open) interval. I didn't subtract 1 second from the new_date_to. OP can present results in any way he wants.

            – Vladimir Baranov
            Apr 3 '17 at 22:32













          • I mean to say from Record ID 3 2017-02-06 08:00:00 | 2017-02-07 00:00:00 the from date and to date is not same, here one second will work fine, but in next row 2017-02-07 00:00:00 | 2017-02-06 00:00:00 its wrong result.

            – MarmiK
            Apr 4 '17 at 10:14











          • Sorry, @MarmiK. I completely missed it. You are right, there was a mistake in the formula that calculates new_date_to. Thank you for pointing it out.

            – Vladimir Baranov
            Apr 4 '17 at 11:05











          • It happens, I does it too :), happy that it's corrected not, I guess the -CA.Number0 (minus) before CA.Number0 was causing this problem.

            – MarmiK
            Apr 5 '17 at 4:51



















          0














          What would the difference be, if i wanted to have the first and last day of the year for a date range of 3 years, 3 times



          StartDate = 1/1/2017
          End Date = 12/31/2020



          Looking for a result of :
          201701201712
          201801201812
          201901201912



          Thanks in advance





          share








          New contributor




          Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "182"
            };
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fdba.stackexchange.com%2fquestions%2f168915%2fsplit-date-range-into-separate-records%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









            4














            One way to do it is to use a table of numbers and CROSS APPLY.



            Sample data



            DECLARE @T TABLE (RecordID int, date_from datetime2(0), date_to datetime2(0));

            INSERT INTO @T (RecordID, date_from, date_to) VALUES
            (1, '2017-02-03 08:00:00' , '2017-02-04 17:00:00'),
            (2, '2017-02-05 08:00:00' , '2017-02-05 17:00:00'),
            (3, '2017-02-06 08:00:00' , '2017-02-10 17:00:00');


            Query



            In this example I generated a table of 10 numbers on the fly (CTE_Numbers). In production I have a permanent table with 100K numbers.



            WITH 
            CTE_Numbers1(n)
            AS
            (
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
            )
            ,CTE_Numbers
            AS
            (
            SELECT ROW_NUMBER() OVER (ORDER BY n) AS Number
            FROM CTE_Numbers1
            )
            SELECT
            T.RecordID
            ,CASE WHEN CA.Number0 = 0
            THEN date_from
            ELSE DATEADD(day, CA.Number0, CAST(T.date_from as date))
            END AS new_date_from
            ,CASE WHEN CA.Number0 = DATEDIFF(day, T.date_from, T.date_to)
            THEN date_to
            ELSE DATEADD(day, CA.Number0 + 1, CAST(T.date_from as date))
            END AS new_date_to
            FROM
            @T AS T
            CROSS APPLY
            (
            SELECT CTE_Numbers.Number - 1 AS Number0
            FROM CTE_Numbers
            WHERE CTE_Numbers.Number <= DATEDIFF(day, T.date_from, T.date_to) + 1
            ) AS CA
            ORDER BY
            RecordID
            ,new_date_from
            ;


            Result



            +----------+---------------------+---------------------+
            | RecordID | new_date_from | new_date_to |
            +----------+---------------------+---------------------+
            | 1 | 2017-02-03 08:00:00 | 2017-02-04 00:00:00 |
            | 1 | 2017-02-04 00:00:00 | 2017-02-04 17:00:00 |
            | 2 | 2017-02-05 08:00:00 | 2017-02-05 17:00:00 |
            | 3 | 2017-02-06 08:00:00 | 2017-02-07 00:00:00 |
            | 3 | 2017-02-07 00:00:00 | 2017-02-08 00:00:00 |
            | 3 | 2017-02-08 00:00:00 | 2017-02-09 00:00:00 |
            | 3 | 2017-02-09 00:00:00 | 2017-02-10 00:00:00 |
            | 3 | 2017-02-10 00:00:00 | 2017-02-10 17:00:00 |
            +----------+---------------------+---------------------+





            share|improve this answer


























            • Great effort, but I think its still not matching the requirements from OP, the date from is greater than date to; Also, date should be same in single row.

              – MarmiK
              Apr 3 '17 at 11:48













            • @MarmiK, the result of my query is [closed; open) interval. I didn't subtract 1 second from the new_date_to. OP can present results in any way he wants.

              – Vladimir Baranov
              Apr 3 '17 at 22:32













            • I mean to say from Record ID 3 2017-02-06 08:00:00 | 2017-02-07 00:00:00 the from date and to date is not same, here one second will work fine, but in next row 2017-02-07 00:00:00 | 2017-02-06 00:00:00 its wrong result.

              – MarmiK
              Apr 4 '17 at 10:14











            • Sorry, @MarmiK. I completely missed it. You are right, there was a mistake in the formula that calculates new_date_to. Thank you for pointing it out.

              – Vladimir Baranov
              Apr 4 '17 at 11:05











            • It happens, I does it too :), happy that it's corrected not, I guess the -CA.Number0 (minus) before CA.Number0 was causing this problem.

              – MarmiK
              Apr 5 '17 at 4:51
















            4














            One way to do it is to use a table of numbers and CROSS APPLY.



            Sample data



            DECLARE @T TABLE (RecordID int, date_from datetime2(0), date_to datetime2(0));

            INSERT INTO @T (RecordID, date_from, date_to) VALUES
            (1, '2017-02-03 08:00:00' , '2017-02-04 17:00:00'),
            (2, '2017-02-05 08:00:00' , '2017-02-05 17:00:00'),
            (3, '2017-02-06 08:00:00' , '2017-02-10 17:00:00');


            Query



            In this example I generated a table of 10 numbers on the fly (CTE_Numbers). In production I have a permanent table with 100K numbers.



            WITH 
            CTE_Numbers1(n)
            AS
            (
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
            )
            ,CTE_Numbers
            AS
            (
            SELECT ROW_NUMBER() OVER (ORDER BY n) AS Number
            FROM CTE_Numbers1
            )
            SELECT
            T.RecordID
            ,CASE WHEN CA.Number0 = 0
            THEN date_from
            ELSE DATEADD(day, CA.Number0, CAST(T.date_from as date))
            END AS new_date_from
            ,CASE WHEN CA.Number0 = DATEDIFF(day, T.date_from, T.date_to)
            THEN date_to
            ELSE DATEADD(day, CA.Number0 + 1, CAST(T.date_from as date))
            END AS new_date_to
            FROM
            @T AS T
            CROSS APPLY
            (
            SELECT CTE_Numbers.Number - 1 AS Number0
            FROM CTE_Numbers
            WHERE CTE_Numbers.Number <= DATEDIFF(day, T.date_from, T.date_to) + 1
            ) AS CA
            ORDER BY
            RecordID
            ,new_date_from
            ;


            Result



            +----------+---------------------+---------------------+
            | RecordID | new_date_from | new_date_to |
            +----------+---------------------+---------------------+
            | 1 | 2017-02-03 08:00:00 | 2017-02-04 00:00:00 |
            | 1 | 2017-02-04 00:00:00 | 2017-02-04 17:00:00 |
            | 2 | 2017-02-05 08:00:00 | 2017-02-05 17:00:00 |
            | 3 | 2017-02-06 08:00:00 | 2017-02-07 00:00:00 |
            | 3 | 2017-02-07 00:00:00 | 2017-02-08 00:00:00 |
            | 3 | 2017-02-08 00:00:00 | 2017-02-09 00:00:00 |
            | 3 | 2017-02-09 00:00:00 | 2017-02-10 00:00:00 |
            | 3 | 2017-02-10 00:00:00 | 2017-02-10 17:00:00 |
            +----------+---------------------+---------------------+





            share|improve this answer


























            • Great effort, but I think its still not matching the requirements from OP, the date from is greater than date to; Also, date should be same in single row.

              – MarmiK
              Apr 3 '17 at 11:48













            • @MarmiK, the result of my query is [closed; open) interval. I didn't subtract 1 second from the new_date_to. OP can present results in any way he wants.

              – Vladimir Baranov
              Apr 3 '17 at 22:32













            • I mean to say from Record ID 3 2017-02-06 08:00:00 | 2017-02-07 00:00:00 the from date and to date is not same, here one second will work fine, but in next row 2017-02-07 00:00:00 | 2017-02-06 00:00:00 its wrong result.

              – MarmiK
              Apr 4 '17 at 10:14











            • Sorry, @MarmiK. I completely missed it. You are right, there was a mistake in the formula that calculates new_date_to. Thank you for pointing it out.

              – Vladimir Baranov
              Apr 4 '17 at 11:05











            • It happens, I does it too :), happy that it's corrected not, I guess the -CA.Number0 (minus) before CA.Number0 was causing this problem.

              – MarmiK
              Apr 5 '17 at 4:51














            4












            4








            4







            One way to do it is to use a table of numbers and CROSS APPLY.



            Sample data



            DECLARE @T TABLE (RecordID int, date_from datetime2(0), date_to datetime2(0));

            INSERT INTO @T (RecordID, date_from, date_to) VALUES
            (1, '2017-02-03 08:00:00' , '2017-02-04 17:00:00'),
            (2, '2017-02-05 08:00:00' , '2017-02-05 17:00:00'),
            (3, '2017-02-06 08:00:00' , '2017-02-10 17:00:00');


            Query



            In this example I generated a table of 10 numbers on the fly (CTE_Numbers). In production I have a permanent table with 100K numbers.



            WITH 
            CTE_Numbers1(n)
            AS
            (
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
            )
            ,CTE_Numbers
            AS
            (
            SELECT ROW_NUMBER() OVER (ORDER BY n) AS Number
            FROM CTE_Numbers1
            )
            SELECT
            T.RecordID
            ,CASE WHEN CA.Number0 = 0
            THEN date_from
            ELSE DATEADD(day, CA.Number0, CAST(T.date_from as date))
            END AS new_date_from
            ,CASE WHEN CA.Number0 = DATEDIFF(day, T.date_from, T.date_to)
            THEN date_to
            ELSE DATEADD(day, CA.Number0 + 1, CAST(T.date_from as date))
            END AS new_date_to
            FROM
            @T AS T
            CROSS APPLY
            (
            SELECT CTE_Numbers.Number - 1 AS Number0
            FROM CTE_Numbers
            WHERE CTE_Numbers.Number <= DATEDIFF(day, T.date_from, T.date_to) + 1
            ) AS CA
            ORDER BY
            RecordID
            ,new_date_from
            ;


            Result



            +----------+---------------------+---------------------+
            | RecordID | new_date_from | new_date_to |
            +----------+---------------------+---------------------+
            | 1 | 2017-02-03 08:00:00 | 2017-02-04 00:00:00 |
            | 1 | 2017-02-04 00:00:00 | 2017-02-04 17:00:00 |
            | 2 | 2017-02-05 08:00:00 | 2017-02-05 17:00:00 |
            | 3 | 2017-02-06 08:00:00 | 2017-02-07 00:00:00 |
            | 3 | 2017-02-07 00:00:00 | 2017-02-08 00:00:00 |
            | 3 | 2017-02-08 00:00:00 | 2017-02-09 00:00:00 |
            | 3 | 2017-02-09 00:00:00 | 2017-02-10 00:00:00 |
            | 3 | 2017-02-10 00:00:00 | 2017-02-10 17:00:00 |
            +----------+---------------------+---------------------+





            share|improve this answer















            One way to do it is to use a table of numbers and CROSS APPLY.



            Sample data



            DECLARE @T TABLE (RecordID int, date_from datetime2(0), date_to datetime2(0));

            INSERT INTO @T (RecordID, date_from, date_to) VALUES
            (1, '2017-02-03 08:00:00' , '2017-02-04 17:00:00'),
            (2, '2017-02-05 08:00:00' , '2017-02-05 17:00:00'),
            (3, '2017-02-06 08:00:00' , '2017-02-10 17:00:00');


            Query



            In this example I generated a table of 10 numbers on the fly (CTE_Numbers). In production I have a permanent table with 100K numbers.



            WITH 
            CTE_Numbers1(n)
            AS
            (
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
            SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
            )
            ,CTE_Numbers
            AS
            (
            SELECT ROW_NUMBER() OVER (ORDER BY n) AS Number
            FROM CTE_Numbers1
            )
            SELECT
            T.RecordID
            ,CASE WHEN CA.Number0 = 0
            THEN date_from
            ELSE DATEADD(day, CA.Number0, CAST(T.date_from as date))
            END AS new_date_from
            ,CASE WHEN CA.Number0 = DATEDIFF(day, T.date_from, T.date_to)
            THEN date_to
            ELSE DATEADD(day, CA.Number0 + 1, CAST(T.date_from as date))
            END AS new_date_to
            FROM
            @T AS T
            CROSS APPLY
            (
            SELECT CTE_Numbers.Number - 1 AS Number0
            FROM CTE_Numbers
            WHERE CTE_Numbers.Number <= DATEDIFF(day, T.date_from, T.date_to) + 1
            ) AS CA
            ORDER BY
            RecordID
            ,new_date_from
            ;


            Result



            +----------+---------------------+---------------------+
            | RecordID | new_date_from | new_date_to |
            +----------+---------------------+---------------------+
            | 1 | 2017-02-03 08:00:00 | 2017-02-04 00:00:00 |
            | 1 | 2017-02-04 00:00:00 | 2017-02-04 17:00:00 |
            | 2 | 2017-02-05 08:00:00 | 2017-02-05 17:00:00 |
            | 3 | 2017-02-06 08:00:00 | 2017-02-07 00:00:00 |
            | 3 | 2017-02-07 00:00:00 | 2017-02-08 00:00:00 |
            | 3 | 2017-02-08 00:00:00 | 2017-02-09 00:00:00 |
            | 3 | 2017-02-09 00:00:00 | 2017-02-10 00:00:00 |
            | 3 | 2017-02-10 00:00:00 | 2017-02-10 17:00:00 |
            +----------+---------------------+---------------------+






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 4 '17 at 11:03

























            answered Apr 3 '17 at 7:29









            Vladimir BaranovVladimir Baranov

            3,69221133




            3,69221133













            • Great effort, but I think its still not matching the requirements from OP, the date from is greater than date to; Also, date should be same in single row.

              – MarmiK
              Apr 3 '17 at 11:48













            • @MarmiK, the result of my query is [closed; open) interval. I didn't subtract 1 second from the new_date_to. OP can present results in any way he wants.

              – Vladimir Baranov
              Apr 3 '17 at 22:32













            • I mean to say from Record ID 3 2017-02-06 08:00:00 | 2017-02-07 00:00:00 the from date and to date is not same, here one second will work fine, but in next row 2017-02-07 00:00:00 | 2017-02-06 00:00:00 its wrong result.

              – MarmiK
              Apr 4 '17 at 10:14











            • Sorry, @MarmiK. I completely missed it. You are right, there was a mistake in the formula that calculates new_date_to. Thank you for pointing it out.

              – Vladimir Baranov
              Apr 4 '17 at 11:05











            • It happens, I does it too :), happy that it's corrected not, I guess the -CA.Number0 (minus) before CA.Number0 was causing this problem.

              – MarmiK
              Apr 5 '17 at 4:51



















            • Great effort, but I think its still not matching the requirements from OP, the date from is greater than date to; Also, date should be same in single row.

              – MarmiK
              Apr 3 '17 at 11:48













            • @MarmiK, the result of my query is [closed; open) interval. I didn't subtract 1 second from the new_date_to. OP can present results in any way he wants.

              – Vladimir Baranov
              Apr 3 '17 at 22:32













            • I mean to say from Record ID 3 2017-02-06 08:00:00 | 2017-02-07 00:00:00 the from date and to date is not same, here one second will work fine, but in next row 2017-02-07 00:00:00 | 2017-02-06 00:00:00 its wrong result.

              – MarmiK
              Apr 4 '17 at 10:14











            • Sorry, @MarmiK. I completely missed it. You are right, there was a mistake in the formula that calculates new_date_to. Thank you for pointing it out.

              – Vladimir Baranov
              Apr 4 '17 at 11:05











            • It happens, I does it too :), happy that it's corrected not, I guess the -CA.Number0 (minus) before CA.Number0 was causing this problem.

              – MarmiK
              Apr 5 '17 at 4:51

















            Great effort, but I think its still not matching the requirements from OP, the date from is greater than date to; Also, date should be same in single row.

            – MarmiK
            Apr 3 '17 at 11:48







            Great effort, but I think its still not matching the requirements from OP, the date from is greater than date to; Also, date should be same in single row.

            – MarmiK
            Apr 3 '17 at 11:48















            @MarmiK, the result of my query is [closed; open) interval. I didn't subtract 1 second from the new_date_to. OP can present results in any way he wants.

            – Vladimir Baranov
            Apr 3 '17 at 22:32







            @MarmiK, the result of my query is [closed; open) interval. I didn't subtract 1 second from the new_date_to. OP can present results in any way he wants.

            – Vladimir Baranov
            Apr 3 '17 at 22:32















            I mean to say from Record ID 3 2017-02-06 08:00:00 | 2017-02-07 00:00:00 the from date and to date is not same, here one second will work fine, but in next row 2017-02-07 00:00:00 | 2017-02-06 00:00:00 its wrong result.

            – MarmiK
            Apr 4 '17 at 10:14





            I mean to say from Record ID 3 2017-02-06 08:00:00 | 2017-02-07 00:00:00 the from date and to date is not same, here one second will work fine, but in next row 2017-02-07 00:00:00 | 2017-02-06 00:00:00 its wrong result.

            – MarmiK
            Apr 4 '17 at 10:14













            Sorry, @MarmiK. I completely missed it. You are right, there was a mistake in the formula that calculates new_date_to. Thank you for pointing it out.

            – Vladimir Baranov
            Apr 4 '17 at 11:05





            Sorry, @MarmiK. I completely missed it. You are right, there was a mistake in the formula that calculates new_date_to. Thank you for pointing it out.

            – Vladimir Baranov
            Apr 4 '17 at 11:05













            It happens, I does it too :), happy that it's corrected not, I guess the -CA.Number0 (minus) before CA.Number0 was causing this problem.

            – MarmiK
            Apr 5 '17 at 4:51





            It happens, I does it too :), happy that it's corrected not, I guess the -CA.Number0 (minus) before CA.Number0 was causing this problem.

            – MarmiK
            Apr 5 '17 at 4:51













            0














            What would the difference be, if i wanted to have the first and last day of the year for a date range of 3 years, 3 times



            StartDate = 1/1/2017
            End Date = 12/31/2020



            Looking for a result of :
            201701201712
            201801201812
            201901201912



            Thanks in advance





            share








            New contributor




            Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

























              0














              What would the difference be, if i wanted to have the first and last day of the year for a date range of 3 years, 3 times



              StartDate = 1/1/2017
              End Date = 12/31/2020



              Looking for a result of :
              201701201712
              201801201812
              201901201912



              Thanks in advance





              share








              New contributor




              Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                0












                0








                0







                What would the difference be, if i wanted to have the first and last day of the year for a date range of 3 years, 3 times



                StartDate = 1/1/2017
                End Date = 12/31/2020



                Looking for a result of :
                201701201712
                201801201812
                201901201912



                Thanks in advance





                share








                New contributor




                Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.










                What would the difference be, if i wanted to have the first and last day of the year for a date range of 3 years, 3 times



                StartDate = 1/1/2017
                End Date = 12/31/2020



                Looking for a result of :
                201701201712
                201801201812
                201901201912



                Thanks in advance






                share








                New contributor




                Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.








                share


                share






                New contributor




                Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 9 mins ago









                Chris KrukemeyerChris Krukemeyer

                1




                1




                New contributor




                Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Chris Krukemeyer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Database Administrators Stack Exchange!


                    • 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%2fdba.stackexchange.com%2fquestions%2f168915%2fsplit-date-range-into-separate-records%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

                    Liste der Baudenkmale in Friedland (Mecklenburg)

                    Single-Malt-Whisky

                    Czorneboh