Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
This is my sql code

 DECLARE @Product TABLE ( Date_From Datetime, Date_To Datetime, Product_ID INT)
INSERT INTO @Product Values
('20110323', '20110326', 101)
INSERT INTO @Product Values
('20110327','20110329',101)

-- The actual solution --
DECLARE @Beg Datetime, @End Datetime
SET @Beg = '20110323'
SET @End = '20110323'

IF @Beg = @End
BEGIN
    DECLARE @Temp TABLE (Date_From Datetime, Date_To Datetime, Product_ID INT, [Version] INT)
    INSERT INTO @Temp
    SELECT p.*, x.[Version]
    FROM @Product AS p, (SELECT 1 AS [Version] UNION SELECT 2 AS [Version]) AS x
    WHERE @Beg BETWEEN p.Date_From AND p.Date_To   

    DELETE FROM @Product WHERE @Beg BETWEEN Date_From AND Date_To   

    UPDATE @temp SET
        Date_From = CASE WHEN [Version] = 1 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, -1, @Beg)
                         WHEN [Version] = 2 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, +1, @Beg)
                         ELSE Date_From END,
        Date_To =  CASE WHEN [Version] = 1 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, -1, @Beg)
                       WHEN [Version] = 2 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, +1, @Beg)
                       ELSE Date_From END
    INSERT INTO @Product ( Date_From, Date_To, Product_ID)
    SELECT  Date_From, Date_To, Product_ID FROM @Temp
END

ELSE

BEGIN
    UPDATE @Product
     SET
        Date_From = CASE WHEN Date_From BETWEEN @Beg AND @End THEN DateAdd(dd, 1, @End) ELSE Date_From END,
        Date_To = CASE WHEN Date_To BETWEEN @Beg AND @End THEN DateAdd(dd, -1, @Beg) ELSE Date_To END
    WHERE Date_From BETWEEN @Beg AND @End
    OR Date_To BETWEEN @Beg AND @End
DELETE FROM @Product WHERE Date_From > Date_To
END

SELECT * FROM @Product ORDER BY Date_From


this is the out put...

2011-03-22 00:00:00.000 2011-03-22 00:00:00.000 101
2011-03-24 00:00:00.000 2011-03-24 00:00:00.000 101
2011-03-27 00:00:00.000 2011-03-29 00:00:00.000 101


problem in this output is 22 that is not in the DECLARE table

Wht i need is below out put

2011-03-24 00:00:00.000 2011-03-26 00:00:00.000 101
2011-03-27 00:00:00.000 2011-03-29 00:00:00.000 101
Posted
Updated 23-Mar-11 2:15am
v2

1 solution

The first impression is that you create the 22th because you first insert 23rd and then you substract 1 day. Try commenting out the
SQL
PDATE @temp SET
        Date_From = CASE WHEN [Version] = 1 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, -1, @Beg)
                         WHEN [Version] = 2 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, +1, @Beg)
                         ELSE Date_From END,
        Date_To =  CASE WHEN [Version] = 1 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, -1, @Beg)
                       WHEN [Version] = 2 AND @Beg BETWEEN Date_From AND Date_To THEN DateAdd(dd, +1, @Beg)
                       ELSE Date_From END

portion to see this.

[Addition]
Based on the desription, could you try something like this:
SQL
DECLARE @Product TABLE ( Date_From Datetime, Date_To Datetime, Product_ID INT)
INSERT INTO @Product Values ('20110323', '20110324', 101);
INSERT INTO @Product Values ('20110325', '20110326', 101);
INSERT INTO @Product Values ('20110327', '20110329', 101);
DECLARE @Beg Datetime, @End Datetime
-- test case 1
SET @Beg = '20110323'
SET @End = '20110323'
-- test case 2
--SET @Beg = '20110324'
--SET @End = '20110327'
-- Set in range dates to null
UPDATE @Product SET 
   Date_From =  CASE 
	               WHEN Date_From BETWEEN @Beg AND @End THEN NULL 
                   ELSE Date_From
                END,
   Date_To =  CASE 
	               WHEN Date_To BETWEEN @Beg AND @End THEN NULL 
                   ELSE Date_To
                END;
-- remove rows having no dates
DELETE FROM @Product 
WHERE Date_From IS NULL
AND Date_To IS NULL;
--update the missing ends to beginning/end correspondingly
UPDATE @Product
SET Date_From =  CASE 
	               WHEN Date_From IS NULL THEN DATEADD(day, +1, @End)
                   ELSE Date_From
                END,
    Date_To =  CASE 
	               WHEN Date_To IS NULL THEN DATEADD(day, -1, @Beg)
                   ELSE Date_To
                END;
                
SELECT * FROM @Product 
ORDER BY Date_From
 
Share this answer
 
v2
Comments
amtechq8 23-Mar-11 8:14am    
tks mr. mika...

Can u help me for editing this query to get my output
Wendelius 23-Mar-11 8:22am    
In order to help you with that we would need to understand the logic, what you're trying to do, not just see the implementation.
amtechq8 23-Mar-11 9:07am    
I have a table "Product" based on datefrom and dateTo

1.for example
Product table Case 1
2011-03-23 2011-03-24 101
2011-03-25 2011-03-26 101
2011-03-27 2011-03-29 101

i want to remove a date from the product table that is 23.
i need output like below

2011-03-24 2011-03-24 101
2011-03-25 2011-03-26 101
2011-03-27 2011-03-29 101

Product table Case 2
2011-03-23 2011-03-24 101
2011-03-25 2011-03-26 101
2011-03-27 2011-03-29 101

i want to remove a date from the product table that is 24 to 27.
i need output like below

2011-03-23 2011-03-23 101
2011-03-28 2011-03-29 101
Wendelius 23-Mar-11 10:46am    
Solution updated
amtechq8 23-Mar-11 17:39pm    
thanks Mr. Mika

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900