I use simple query to get dates of contract for a person:
SELECT
[KlienciUmowyDataPoczatkowa],
IsNULL([KlienciUmowyDataKoncowa], GETDATE()) AS 'KlienciUmowyDataKoncowa'
FROM [BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE [PortfelID] = 3
This is what I get:
KlienciUmowyDataPoczatkowa KlienciUmowyDataKoncowa
2005-11-28 00:00:00.000 2008-07-22 00:00:00.000
2008-07-23 00:00:00.000 2010-03-09 15:45:42.457
Client signs a contract which started at 2005-11-28 and then appendix was signed so his currenct contract ended 2008-07-22 and new one started at 2008-07-23 and lasts till today (NULL that was converted to current time). There can be clients with many many more appendix but it all goes like that.
My question is
: How do i get contract that is/was active between lets say 2008-04-01 - 2008-06-30? It's possible that the client between that period will have 2 or even 5 appendix so it should return all of them.
Also I am not sure if it's needed to use IsNull? Maybe there's a better way for this so i could skip IsNull usage and entering current date as replacement.
EDIT:
I thought that solution from marc_s solved it but it seems it didn't:
SELECT
[KlienciUmowyDataPoczatkowa],
[KlienciUmowyDataKoncowa]
FROM [BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE [PortfelID] = 3 AND
[KlienciUmowyDataPoczatkowa] <= '2008-07-01' AND IsNULL([KlienciUmowyDataKoncowa], '99991231') >= '2008-09-30'
It return 0 dates for the period of 2008-07-01 to 2008-09-30 when it should return both:
2005-11-28 00:00:00.000 2008-07-22 00:00:00.000
2008-07-23 00:00:00.000 NULL
As the client had contract in both times.
EDIT2:
I've tested proposed 2 queries. First one for dates (20080401 - 20080630) as in example below returns 1 row for 1st query (expected), returns 0 rows for 2nd query (not expected).
SELECT [KlienciUmowyDataPoczatkowa],
IsNULL([KlienciUmowyDataKoncowa], '99991231') AS 'KlienciUmowyDataKoncowa'
FROM [BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE [PortfelID] = 3
AND [KlienciUmowyDataPoczatkowa] <= '20080401' AND IsNULL([KlienciUmowyDataKoncowa], '99991231') >= '20080630'
SELECT
[KlienciUmowyDataPoczatkowa],
[KlienciUmowyDataKoncowa]
FROM
[BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE
[PortfelID] = 3 AND
-- either: start date is sometime between the two dates
([KlienciUmowyDataPoczatkowa] BETWEEN '20080401' AND '20080630'
-- or: end date is sometime between the two dates
OR
ISNULL([KlienciUmowyDataKoncowa], GETDATE()) BETWEEN '20080401' AND '20080630')
Second test for dates '20080701' to '20080930' shows for first query 0 results (not expected), and for 2nd query 2 rows (expected).
SELECT [KlienciUmowyDataPoczatkowa],
IsNULL([KlienciUmowyDataKoncowa], '99991231') AS 'KlienciUmowyDataKoncowa'
FROM [BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE [PortfelID] = 3
AND [KlienciUmowyDataPoczatkowa] <= '20080701' AND IsNULL([KlienciUmowyDataKoncowa], '99991231') >= '20080930'
SELECT
[KlienciUmowyDataPoczatkowa],
[KlienciUmowyDataKoncowa]
FROM
[BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE
[PortfelID] = 3 AND
-- either: start date is sometime between the two dates
([KlienciUmowyDataPoczatkowa] BETWEEN '20080701' AND '20080930'
-- or: end date is sometime between the two dates
OR
ISNULL([KlienciUmowyDataKoncowa], GETDATE()) BETWEEN '20080701' AND '20080930')
EDIT3:
Using COMBINED solution from both examples it works for both dates. But won't it blow back at me for diffrent client dates? Any Ideas?
SELECT [KlienciUmowyDataPoczatkowa]
, ISNULL([KlienciUmowyDataKoncowa], GETDATE()) AS 'KlienciUmowyDataKoncowa'
FROM [BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE [PortfelID] = 3
AND
(
([KlienciUmowyDataPoczatkowa] BETWEEN '20080401' AND '20080630'
OR ISNULL([KlienciUmowyDataKoncowa], GETDATE()) BETWEEN '20080401' AND '20080630')
OR ([KlienciUmowyDataPoczatkowa] <= '20080401' AND IsNULL([KlienciUmowyDataKoncowa], '99991231') >= '20080630')
)
SELECT [KlienciUmowyDataPoczatkowa]
, ISNULL([KlienciUmowyDataKoncowa], GETDATE()) AS 'KlienciUmowyDataKoncowa'
FROM [BazaZarzadzanie].[dbo].[KlienciPortfeleUmowy]
WHERE [PortfelID] = 3
AND
(
([KlienciUmowyDataPoczatkowa] BETWEEN '20080701' AND '20080930'
OR ISNULL([KlienciUmowyDataKoncowa], GETDATE()) BETWEEN '20080701' AND '20080930')
OR ([KlienciUmowyDataPoczatkowa] <= '20080701' AND IsNULL([KlienciUmowyDataKoncowa], '99991231') >= '20080930')
)