Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
this is the table

DECLARE @Product TABLE (Date_From Datetime, Date_To Datetime, Product_ID INT)
INSERT INTO @Product Values
('20110320', '20110330', 101)


DECLARE @Beg Datetime, @End Datetime
SET @Beg = '20110324'
SET @End = '20110326'


i wand to split my date range 20 to 30 by 24 to 26



I need my out put like this

20/03/2011  23/03/2011 101

27/03/2011  30/03/2011 101


what is the sql query for this
Posted

1 solution

Have to ask: Was there something wrong/not working with the answer in How to update a date range?[^] or is this a different situation?

[Addition]
Since this was a new case, perhaps something like this:
SQL
DECLARE @Product TABLE (Date_From Datetime, Date_To Datetime, Product_ID INT)
INSERT INTO @Product Values ('20110320', '20110330', 101)
DECLARE @Beg Datetime, @End Datetime
SET @Beg = '20110324'
SET @End = '20110326'

DECLARE @Temp TABLE (DateVal  Datetime, Date_Type int, Product_ID INT)

-- insert each date to temp tabl
insert into @Temp
select Date_From, 1, Product_ID
from @Product
union all
select Date_To, 2, Product_ID
from @Product

-- add the date variables to temp table NOTE fixed product so it possibly comes from a variable etc.
insert into @Temp
select @Beg, 2, 101
union all
select @End, 1, 101

-- remove existing rows, most likely here should be a WHERE clause restricting the deletion
delete from @Product;

-- recreate the product table based on temp table
insert into @Product
select  q1.dateval,
        ( select min(q2.dateval)
          from @Temp q2
          where q2.Date_type = 2
          and q2.dateval > q1.dateval),
        q1.Product_ID
from @Temp q1
where q1.Date_type = 1

SELECT * FROM @Product ORDER BY Date_From

Most likely you have to add different kinds of where clauses etc to make this fit into your needs but since I had only 1 test case data I verified the behaviour against it.

Hopefully it helps.
 
Share this answer
 
v3
Comments
amtechq8 23-Mar-11 17:33pm    
1. already get the old issue problem only remaining this
2. now i have only one issue that is this...
Wendelius 23-Mar-11 17:41pm    
Ok, just a note: voting or marking the solution as an answer would help to know if the solution provided was helpful.

About this issue, can there be several rows in the db that cover the date range in the variables? For example could you have rows:
- 20110320 20110330
- 20110325 20110330 etc.
amtechq8 23-Mar-11 17:47pm    
This is the first issue

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

this working fine...
------------------------------------



Issue 2
INSERT INTO @Product Values ('20110323', '20110330', 101);
SET @Beg = '20110325'
SET @End = '20110327'


i need out put like this

2011-03-23 2011-03-24 101
2011-03-28 2011-03-30 101
Wendelius 23-Mar-11 18:11pm    
Solution updated
amtechq8 23-Mar-11 18:32pm    
thanks for the solution...

is it possible to fix this both issues in one sql code

Issue 1
INSERT INTO @Product Values ('20110323', '20110324', 101);
INSERT INTO @Product Values ('20110325', '20110326', 101);
INSERT INTO @Product Values ('20110327', '20110331', 101);

DECLARE @Beg Datetime, @End Datetime
SET @Beg = '20110328'
SET @End = '20110329'

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