Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Other Technologies / English Query / July 2008

Tip: Looking for answers? Try searching our database.

datetime comparison broken???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan - 08 Jul 2008 00:49 GMT
I've spent a few hours researching the datetime comparison, and from what I
understand, what I'm doing *should* be working... any help is GREATLY
appreciated.

I have a table that stores a column (EventDateTime) in the datetime format.  
If I run a query in query analyzer, the column returns values like 2008-07-07
16:11:19, so from what I understand, this is valid datetime format.  

I'm simply trying to filter my query results to only include events that
happened within the last 7 days.  I've tried a half dozen different ways of
doing this, but I always get items in the query results that are well over 7
days old.  If I change the query to be items less than 7 days old, it seems
to return the right values.  The results when trying to retrieve only items
newer then 7 days consistently returns items that are years old.  For example
I get '2006-05-20 16:11:19.000'

some things I've tried are (please note that when I have the "and" below,
I'm just showing different WHERE statements that I've tried, NOT that I'm
actually trying to combine them in one query :-))

<select statement>
WHERE EventDateTime > dateadd("d",-7,getdate())

and
WHERE EventDateTime > dateadd(dd, -7, getdate())

and
WHERE EventDateTime > CAST(dateadd("d",-7,getdate()) AD datetime)

and
WHERE convert(datetime,convert(varchar,EventDateTime,121)) >
convert(datetime,convert(varchar,dateadd("d",-7,getdate()),121))

and
WHERE CONVERT(VARCHAR,EventDateTime,112) > 20080707

All of these consistently return (the same) items that are over the 7 days
(as well as the items within the 7 day window).  I've even tried a few
queries with the date set manually.  Oh, I also tried converting all of the
values to VARCHAR for comparison, but no dice there either.  

If I just return the value of one entry
SELECT EventDateTime from Events WHERE AutoID = '30888' I get
'2006-05-20 16:11:19.000'
and if I do
SELECT getdate() I get
'2008-07-07 19:39:21.640'

but if I filter with
WHERE EventDateTime > getdate()
that 30888 entry (along with 7k others is returned... I'd expect nothing
returned...

I'm pulling my hair out here.  From EVERYTHING I'm seeing, this should be a
straight forward query, but I've spent HOURS on it.  Any help is GREATLY
appreciated.

-Dan
Dan - 08 Jul 2008 01:11 GMT
I am a **COMPLETE** IDIOT...
A few hours wasted... fun..

I had an OR statement in the WHERE test that I overlooked which caused the
extra results to be shown....

> I've spent a few hours researching the datetime comparison, and from what I
> understand, what I'm doing *should* be working... any help is GREATLY
[quoted text clipped - 54 lines]
>
> -Dan
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.