Solved

SQL Date Period

Posted on 2009-07-15
8
378 Views
Last Modified: 2012-05-07
Hi Experts,

I've a problem getting some data from a table. The user enters a period of time. Then the query should look up in a table if there are records which:
- began before the entered period and end after the entered period
- began before the entered period and end in the entered period
- began before the entered period and have no end date (NULL)
- began in the entered period and end after the entered period
- began in the entered period and end in the entered period
- began in the entered period and have no end date (NULL)

Table structure:

ID      |       Desc       |       DateStart       |       DateEnd
--------------------------------------------------------------------
1       |        Inv1        |        01/02/08       |       30/04/09
2       |        Inv2        |        01/01/09       |       31/12/10
3       |        Inv3        |        01/02/08       |       NULL
4       |        Inv4        |        01/02/08       |       30/06/08

Now, with an entered period of: 01/01/09 - 31/07/09, the query should return:

ID      |       Desc       |       DateStart       |       DateEnd
--------------------------------------------------------------------
1       |        Inv1        |        01/02/08       |       30/04/09
2       |        Inv2        |        01/01/09       |       31/12/10
3       |        Inv3        |        01/02/08       |       NULL

Can anybody help me with this?

Regards,
Zoidi
0
Comment
Question by:zoidi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24857336
this logic is what you are looking for:
where ( @date start <= DateEnd  DateEnd IS NULL )
  and @date_end >= DateStart
  

Open in new window

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24857339
sorry:
where ( @date start <= DateEnd OR  DateEnd IS NULL )
  and @date_end >= DateStart

Open in new window

0
 
LVL 1

Author Comment

by:zoidi
ID: 24857392
Hi,

Thank you for your fast answer. It is almost correct except that it does not return records where DateEnd is NULL. Could you figure this out?

Thank you,
Zoidi
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24857461
I think you just need to flip the DateEnd IS NULL in front.
where ( DateEnd IS NULL OR @date_start <= DateEnd )
  and @date_end >= DateStart
 
-- or alternatively
where @date_start <= coalesce(DaateEnd, @dae_start)
  and @date_end >= DateStart

Open in new window

0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
ID: 24857469
type-o's
where @date_start <= coalesce(DateEnd, @date_start)
  and @date_end >= DateStart

Open in new window

0
 
LVL 1

Author Closing Comment

by:zoidi
ID: 31603624
Thank you guys for your help. It worked just fine :)
0
 
LVL 1

Author Comment

by:zoidi
ID: 24857513
Oh, sorry: The accepted solution should be mwvisa1's comment and the assisted solution should be angellll's

Regards,
Zoidi
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24857537
Glad it helped.  

I was merely trying to assist Angel Eyes anyway, so no worries on my end.  However, good note/clarification for future readers, though, to indicate which code you used as your actual solution.  

Happy coding!

Regards,
K
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question