Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:09:22+00:00 2026-05-30T13:09:22+00:00

I have a table stored on a SQL Server 2008, that associate a value

  • 0

I have a table stored on a SQL Server 2008, that associate a value to a date range.

DateFrom      DateTo         Value
2012-01-01    2012-02-01      10
2012-02-02    2012-02-15      15

The application that deal with this table, can insert a new range between the existings.
For example, If i insert

DateFrom      DateTo         Value
2012-02-07    2012-02-10      12

The result must be

DateFrom      DateTo         Value
2012-01-01    2012-02-01      10
2012-02-02    2012-02-06      15
2012-02-07    2012-02-10      12
2012-02-11    2012-02-15      15

I can do that programmatically from the application, but I wonder if there is some fast SQL statement that make me able to set the data values by referencing other row’s field and performing data operation on it.

A MUST requirement is that the date range must represent a time sequence, two range cannot span each other.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:09:23+00:00Added an answer on May 30, 2026 at 1:09 pm

    I’ve had similar problems in the past, and found that if the range needs to be continuous the best approach is to do away with the End Date of the range, and calculate this as the Next start date. Then if needs be create a view as follows:

    SELECT  FromDate,
            (   SELECT  DATEADD(DAY, -1, MIN(DateFrom))
                FROM    YourTable b
                WHERE   b.FromDate > a.FromDate
            ) [ToDate],
            Value
    FROM    YourTable a
    

    This ensures that 2 ranges can never cross, however does not necessarily ensure no work is required upon insert to get the desired result, but it should be more maintainable and have less scope for error than storing both the start and end date.

    ADDENDUM

    Once I had written out all of the below I realised it does not improve maintainability that much to do away with the DateTo Field, it still requires a fair amount of code for the validation, but here’s how I would do it anyway.

    DECLARE  @T table (DateFrom DATE, Value INT)
    INSERT INTO @T VALUES ('20120101', 10), ('20120202', 15), ('20120207', 12), ('20120211', 15)
    
    DECLARE @NewFrom DATE = '20120209',
            @NewTo DATE = '20120210',
            @NewValue INT = 8
    
    -- SHOW INITIAL VALUES FOR DEMONSTATIVE PURPOSES -- 
    SELECT  DateFrom,
            ISNULL((   SELECT  DATEADD(DAY, -1, MIN(DateFrom))
                        FROM    @t b
                        WHERE   b.DateFrom > a.DateFrom
                    ), CAST(GETDATE() AS DATE)) [DateTo],
            Value
    FROM    @t a
    ORDER BY DateFrom
    
    ;WITH CTE AS
    (   SELECT  DateFrom,
                (   SELECT  DATEADD(DAY, -1, MIN(DateFrom))
                    FROM    @t b
                    WHERE   b.DateFrom > a.DateFrom
                ) [DateTo],
                Value
        FROM    @t a    
    ), 
    MergeCTE AS
    (   SELECT  @NewFrom [DateFrom], @NewValue [Value], 'INSERT' [RowAction]
        WHERE   @NewFrom < @NewTo   -- ENSURE A VALID RANGE IS ENTERED
        UNION ALL 
        -- INSERT A ROW WHERE THE NEW DATE TO SLICES AN EXISTING PERIOD
        SELECT  DATEADD(DAY, 1, @NewTo), Value, 'INSERT'
        FROM    CTE
        WHERE   @NewTo BETWEEN DateFrom AND DateTo
        UNION ALL 
        -- DELETE ALL ENTRIES STARTING WITHIN THE DEFINED PERIOD
        SELECT  DateFrom, Value, 'DELETE'
        FROM    CTE
        WHERE   DateFrom BETWEEN @NewFrom AND @NewTo
    )
    MERGE INTO @t t USING MergeCTE c ON t.DateFrom = c.DateFrom AND t.Value = c.Value
    WHEN MATCHED AND RowAction = 'DELETE' THEN DELETE
    WHEN NOT MATCHED THEN INSERT VALUES (c.DateFrom, c.Value);
    
    SELECT  DateFrom,
            ISNULL((   SELECT  DATEADD(DAY, -1, MIN(DateFrom))
                        FROM    @t b
                        WHERE   b.DateFrom > a.DateFrom
                    ), CAST(GETDATE() AS DATE)) [DateTo],
            Value
    FROM    @t a
    ORDER BY DateFrom
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server 2008 stored procedure that updates values in a table.
I have a SQL Server 2008 database. This database has a stored procedure that
I have a database table in Sql Server 2008 R2 which contains data stored
I have a stored procedure in SQL Server 2008 that will insert a record
I have been investigating Table-Valued Parameters in SQL Server 2008, and I've discovered that
I have a stored procedure that retrieves sensitive information from an SQL Server 2008
I have an ASP.NET MVC Web Application that interacts with a SQL Server 2008
I have a stored procedure on SQL Server 2008 that copies certain values from
I have an sql server 2008 db table that holds links to articles. My
Ok I have a table in my SQL Server database that stores comments. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.