Solved

SQL Date Period

Posted on 2009-07-15
8
367 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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 142

Accepted Solution

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

Open in new window

0
 
LVL 1

Author Comment

by:zoidi
Comment Utility
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
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 250 total points
Comment Utility
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
Comment Utility
Thank you guys for your help. It worked just fine :)
0
 
LVL 1

Author Comment

by:zoidi
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now