January 17, 2013 at 4:40 am
Hello,
I am trying to create a query to get the results for yesterdays data only. So basically i need all the sales for yesterdays invoices date. I am running the query and getting zero results. The SQL i am using is below. Does anyone know if i am doing this wrong?
SELECT [Invoice_Date]
,[UserId]
,[Customer]
,[InvoiceNo]
,[Description]
,[Pack]
,[PIP_Code]
,[Class]
,[Major_Grp]
,[Invoice_Val]
,[Cost_Pr]
,[Quantity]
FROM [Telesales].[dbo].[Sales]
Where DATEADD(d,-1,GETDATE()) = Invoice_Date
Also tried:
SELECT [Invoice_Date]
,[UserId]
,[Customer]
,[InvoiceNo]
,[Description]
,[Pack]
,[PIP_Code]
,[Class]
,[Major_Grp]
,[Invoice_Val]
,[Cost_Pr]
,[Quantity]
FROM [Telesales].[dbo].[Sales]
Where Invoice_Date = (GETDATE() - 1)
January 17, 2013 at 4:42 am
it's not work properly............
January 17, 2013 at 4:47 am
Thats becuase you probably wont have anything that is equal to the date being used in the where clause
You really need to do an >= < where clause
SELECT [Invoice_Date]
,[UserId]
,[Customer]
,[InvoiceNo]
,[Description]
,[Pack]
,[PIP_Code]
,[Class]
,[Major_Grp]
,[Invoice_Val]
,[Cost_Pr]
,[Quantity]
FROM [Telesales].[dbo].[Sales]
Where Invoice_Date >= dateadd(dd, datediff(dd, 0, GETDATE()) - 1, 0)
AND Invoice_Date < dateadd(dd, datediff(dd, 0, GETDATE()), 0)
Remember that dates are forever moving values
No two runs of the query are the same when using = and getdate() as the time stamp will change on each run.
January 17, 2013 at 7:03 am
If your field is a datetime data type, then you're comparing the datetime result of GetDate() against the datetime value of the field. So, nothing's going to match. I like the BETWEEN or <= and >= approach as well. That was you can avoid putting date parsing functions on the field (i.e.: to the left of the = sign) which would be a disaster for performance.
Effectively, you want use date functions that evaluate to something like this:
WHERE Invoice_Date BETWEEN '01/01/2013 00:00:00.000'
AND '01/01/2013 23:59:59.999'
January 17, 2013 at 7:08 am
Ed Wagner (1/17/2013)
If your field is a datetime data type, then you're comparing the datetime result of GetDate() against the datetime value of the field. So, nothing's going to match. I like the BETWEEN or <= and >= approach as well. That was you can avoid putting date parsing functions on the field (i.e.: to the left of the = sign) which would be a disaster for performance.Effectively, you want use date functions that evaluate to something like this:
WHERE Invoice_Date BETWEEN '01/01/2013 00:00:00.000'
AND '01/01/2013 23:59:59.999'
That will equate to BETWEEN 01/01/2013 and 02/01/2013 due to the rounding of datetime.
You would want 01/01/2013 00:00:00.000 AND 01/01/2013 23:59:59.997 instead, unless your using datetime2
But always try to remember to qualify your dates as ISO formats so that SQL doesnt get confused or set the dateformat to the correct format
YYYY-MM-DDTHH:MM:SS.ms
Take 02/01/2013 is it 2nd Jan 2013 or 1st Feb 2013, depending who you ask you will get a different answer, where as if it follows an ISO standard there cannot be any confusion
January 17, 2013 at 7:13 am
Thanks Anthony Green. That worked fine.
January 17, 2013 at 7:15 am
ziako (1/17/2013)
Thanks Anthony Green. That worked fine.
No problem, glad I could assist.
May I suggest some further reading http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/
They may help you out with other date related issues you may run into.
January 18, 2013 at 9:29 am
anthony.green (1/17/2013)
Ed Wagner (1/17/2013)
If your field is a datetime data type, then you're comparing the datetime result of GetDate() against the datetime value of the field. So, nothing's going to match. I like the BETWEEN or <= and >= approach as well. That was you can avoid putting date parsing functions on the field (i.e.: to the left of the = sign) which would be a disaster for performance.Effectively, you want use date functions that evaluate to something like this:
WHERE Invoice_Date BETWEEN '01/01/2013 00:00:00.000'
AND '01/01/2013 23:59:59.999'
That will equate to BETWEEN 01/01/2013 and 02/01/2013 due to the rounding of datetime.
You would want 01/01/2013 00:00:00.000 AND 01/01/2013 23:59:59.997 instead, unless your using datetime2
But always try to remember to qualify your dates as ISO formats so that SQL doesnt get confused or set the dateformat to the correct format
YYYY-MM-DDTHH:MM:SS.ms
Take 02/01/2013 is it 2nd Jan 2013 or 1st Feb 2013, depending who you ask you will get a different answer, where as if it follows an ISO standard there cannot be any confusion
It would be even better to specify start and end dates using the "half-open interval" model, like this:
WHERE DatetimeColumn >= '2013-01-01 00:00:00.000' AND DatetimeColumn < '2013-01-02 00:00:00.000'
This will return all values with a date component of 2013-01-01 with none of the ambiguity that can result from rounding the time component when evaluating the BETWEEN condition.
It's called a "half-open interval" because the "start" datetime is included in the range but the "end" datetime is not.
Jason Wolfkill
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply