Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Why does my yesterday query return no results? Expand / Collapse
Author
Message
Posted Tuesday, October 23, 2012 7:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 3:01 AM
Points: 147, Visits: 161
Hi all I have a datetime column in my DB and I am trying to fetch all records from yesterday using

SELECT *from SLADB.dbo.DocketTB
WHERE Docket_Date = (GETDATE() - 1)


but is always returns no results and I no the data is there

2012-09-22 21:33:39.390

is this because of the time ?


Thanks
Post #1376022
Posted Tuesday, October 23, 2012 7:18 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 3:01 AM
Points: 147, Visits: 161
where Docket_Date = DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1

Post #1376024
Posted Tuesday, October 23, 2012 7:23 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
Try casting both sides of the comparison as date:

WHERE CAST(Docket_Date AS DATE) = CAST(GETDATE() AS DATE) - 1



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1376029
Posted Tuesday, October 23, 2012 7:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 3:01 AM
Points: 147, Visits: 161
Operand type clash: date is incompatible with int


Is it the time thats causing the problem ?
Post #1376031
Posted Tuesday, October 23, 2012 7:32 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 3:01 AM
Points: 147, Visits: 161
SELECT *from SLADB.dbo.DocketTB


where Docket_EngFinish >=dateadd(d,datediff(d,0, getdate()),-1) AND Docket_EngFinish<dateadd(d,datediff(d,0, getdate()),0)


This works, is there a better way ?

Thanks again Chris
Post #1376040
Posted Tuesday, October 23, 2012 7:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
jerome.morris (10/23/2012)
SELECT *from SLADB.dbo.DocketTB


where Docket_EngFinish >=dateadd(d,datediff(d,0, getdate()),-1) AND Docket_EngFinish<dateadd(d,datediff(d,0, getdate()),0)


This works, is there a better way ?

Thanks again Chris


Yes - cast both sides as DATE

where CAST(Docket_EngFinish AS DATE) = CAST(GETDATE() AS DATE) - 1

Does Docket_EngFinish have a time component or is it all 0's?


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1376051
Posted Tuesday, October 23, 2012 7:50 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 19, 2014 3:01 AM
Points: 147, Visits: 161
It has a time stamp also and the cast gave me the error

Operand type clash: date is incompatible with int
Post #1376054
Posted Tuesday, October 23, 2012 7:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:33 AM
Points: 6,754, Visits: 12,854
Oops...

CAST(GETDATE() -1 AS DATE)


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1376064
Posted Tuesday, October 23, 2012 9:08 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 4:48 AM
Points: 946, Visits: 2,531
Hi

Did you try DATEDIFF(day, Docket_EngFinish , getdate())=1 ?
The datediff by day checks only the day part and not the time.
http://msdn.microsoft.com/en-us/library/ms189794.aspx
Post #1376121
Posted Tuesday, October 23, 2012 8:43 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:19 AM
Points: 35,955, Visits: 30,245
ChrisM@Work (10/23/2012)
Yes - cast both sides as DATE


I don't believe so, Chris... no chance of getting an index seek that way.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1376303
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse