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
Solved

SQL Date Period

Posted on 2009-07-15
8
375 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
  • 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 59

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 59

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 59

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

829 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