Solved

trying to get date range to report

Posted on 2011-02-21
7
1,028 Views
Last Modified: 2013-11-15
I am trying to get reports to print out according to dates - the issue is that it is not an absolute between this date and that date - it is a "is this report active during the dates given"
So the reports could start before the "date entered" the reports could end before the "date entered" but can never start before the end date and never end before the start date.
I have been working on this for 8 hours and am chasing myself in circles

scenarios
Please see image -

query

SELECT *
WHERE ((cMinStart <= '''+ @start +''' AND cMinStart <= '''+ @end +''' AND cMaxEnd <= '''+@end+''' AND cMaxEnd >= '''+@start+''')
      OR (cMinStart <= '''+ @start +''' AND cMinStart <= '''+ @end +''' AND cMaxEnd >= '''+@start+''' OR cMaxEnd >= '''+@end+''' OR cMaxEnd <= '''+@end+''')
      OR (cMinStart <= '''+ @start +''' AND cMinStart <= '''+ @end +''' AND cMaxEnd >= '''+@start+''' AND cMaxEnd >= '''+@end+''')
      OR (cMinStart <= '''+ @start +''' AND cMinStart <= '''+ @end +''' AND cMaxEnd >= '''+@start+''' AND cMaxEnd <= '''+@end+''')
      OR (cMinStart  >= '''+ @start +''' AND cMaxEnd <= '''+@end+'''))
      
table information
these date line up as written in columns

clabel
An important message... (2/18/11)
An important message... (2/18/11)
Planned Service Interruption Notification (2/21/11
Ends but includes Today
flanky doodles
Starts and includes Today
This Month
Today
Tomorrow

cMinStart
2011-02-18 13:36:00.437
2011-02-21 08:52:17.537
2011-02-21 08:28:06.570
2011-02-21 14:22:38.700
2011-02-21 09:09:46.920
2011-02-18 00:00:00.000
2011-02-01 00:00:00.000
2011-02-21 00:00:00.000
2011-02-22 00:00:00.000

cMaxStart
2040-01-01 00:00:00.000
2040-01-01 00:00:00.000
2040-01-01 00:00:00.000
2040-01-01 00:00:00.000
2040-01-01 00:00:00.000
2040-01-01 00:00:00.000
2011-03-01 00:00:00.000
2011-02-22 00:00:00.000
2040-01-01 00:00:00.000
0
Comment
Question by:tagomtech
  • 6
7 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 34947493
I did not see any image. Is the data provided, a sample from your table. Can you provide the table definition. Also post the expected result.
0
 

Author Comment

by:tagomtech
ID: 34951770
1. posting image!
2. yes the data is a sample but there are other things that are not needed that scripting it out would give.
clabael is a varchar
cminMax is a datetime (cast to a varchar in the stored procedure)
cminStart is a datetime (cast to a varchar in st proc)

If there is an easier way to do this I am all ears, my boss suggested using or and not
the results are seen in the pic - if there is a yes then that report should be pulled in when @start and @end are the same day - this is the time frame i am having trouble with.



possiblescenarios.PNG
0
 

Author Comment

by:tagomtech
ID: 34952290

table definition
CREATE TABLE [dbo].[DateTest_campaigns](
      [cLabel] [varchar](50) NOT NULL,
      [cMinStart] [datetime] NOT NULL,
      [cMaxEnd] [datetime] NOT NULL
) ON [PRIMARY]

GO

data in file
campaigntestdata.csv
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:tagomtech
ID: 34953165
Corrected Data and result picture - REALLY need help with this
In the picture the rows highlighted in blue should not be included in a query returning todays dates -
Also uploading the correct datafile for the table so that the dates match what i am testing with

criteria for search
@start = '2011-02-22 00:00:00'
@end = '2011-02-22 23:59:59'

campaigntestdata-correct.csv
0
 

Author Comment

by:tagomtech
ID: 34953173
expected results - file returned without the blue highlights
results.JPG
0
 

Accepted Solution

by:
tagomtech earned 0 total points
ID: 34953723
solution

WHERE (((cMinStart <= '''+ @start +''' AND cMinStart !> '''+ @end +''' AND cMaxEnd > '''+@start+''') AND (cMaxEnd <= '''+@end+''' OR cMaxEnd >= '''+@start+'''))
      OR (cMinStart  >= '''+ @start +''' AND cMaxEnd <= '''+@end+'''))
0
 

Author Closing Comment

by:tagomtech
ID: 34990810
solved myself
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

919 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

16 Experts available now in Live!

Get 1:1 Help Now