Need to Split a Date Column into 2 solumn as Start Date and End Date

  • I need help over this thing. I am working on a table which looks like

    Date Dateofyear

    2014-01-01 1

    2014-01-02 2

    2014-01-03 3

    2014-01-04 4

    2014-01-05 5

    . .

    ..

    ..

    2014-12-31 365

    Now i need to Run a query on this table to show me result after taking input from user (store proc ) if user enters 2 then

    StartDate EndDate

    2014/01/01 2014/07/01

    2014-07-02 2014-12-31

    and if user enter 5 then

    StartDate EndDate

    2014/01/01 2014-03-14

    2014-03-15 2014-05-26

    2014-05-27 2014-08-07

    2014-08-08 2014-10-19

    2014-10-19 2014-12-31

    I hope I made it clear; kindly Help me over this.

    Alot of thanks in Advance ;

    please

  • This can easily be achieved by using NTILE.

    DECLARE @parts INT = 5;

    SELECT

    StartDate= MIN([Date])

    ,EndDate= MAX([Date])

    FROM

    (

    SELECT

    [Date]

    ,Parts = NTILE(@parts) OVER (ORDER BY [DateOfYear])

    FROM [dbo].[myDateTable]

    WHERE [Year] = 2014

    ) tmp

    GROUP BY Parts;

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply