October 23, 2012 at 7:14 am
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
October 23, 2012 at 7:18 am
where Docket_Date = DATEADD(d,DATEDIFF(d,0,GETDATE()),0)-1
October 23, 2012 at 7:23 am
Try casting both sides of the comparison as date:
WHERE CAST(Docket_Date AS DATE) = CAST(GETDATE() AS DATE) - 1
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
October 23, 2012 at 7:26 am
Operand type clash: date is incompatible with int
Is it the time thats causing the problem ?
October 23, 2012 at 7:32 am
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 🙂
October 23, 2012 at 7:47 am
jerome.morris (10/23/2012)
SELECT *from SLADB.dbo.DocketTBwhere 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?
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
October 23, 2012 at 7:50 am
It has a time stamp also and the cast gave me the error
Operand type clash: date is incompatible with int
October 23, 2012 at 7:57 am
Oops...
CAST(GETDATE() -1 AS DATE)
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
October 23, 2012 at 9:08 am
Hi
Did you try DATEDIFF(day, Docket_EngFinish , getdate())=1 ?
The datediff by day checks only the day part and not the time.
October 23, 2012 at 8:43 pm
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
Change is inevitable... Change for the better is not.
October 23, 2012 at 8:44 pm
jerome.morris (10/23/2012)
It has a time stamp also and the cast gave me the errorOperand type clash: date is incompatible with int
Wait a minute now... are you saying that one of the date columns is an INT that looks like a date?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 24, 2012 at 2:34 am
Jeff Moden (10/23/2012)
jerome.morris (10/23/2012)
It has a time stamp also and the cast gave me the errorOperand type clash: date is incompatible with int
Wait a minute now... are you saying that one of the date columns is an INT that looks like a date?
Jeff, you can add an INT to a DATETIME but not a DATE.
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
October 24, 2012 at 2:36 am
Jeff Moden (10/23/2012)
ChrisM@Work (10/23/2012)
Yes - cast both sides as DATEI don't believe so, Chris... no chance of getting an index seek that way.
That's what I thought too until recently.
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
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply