Solved

SQL Date Period

Posted on 2009-07-15
8
377 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

729 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