Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
Get date not working
Get date not working
Rate Topic
Display Mode
Topic Options
Author
Message
dbman
dbman
Posted Thursday, January 17, 2013 4:40 AM
SSC Journeyman
Group: General Forum Members
Last Login: Today @ 3:39 AM
Points: 75,
Visits: 1,080
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)
Post #1408326
alpeshchauhan
alpeshchauhan
Posted Thursday, January 17, 2013 4:42 AM
Forum Newbie
Group: General Forum Members
Last Login: Monday, January 28, 2013 1:38 AM
Points: 7,
Visits: 27
it's not work properly............
Post #1408328
anthony.green
anthony.green
Posted Thursday, January 17, 2013 4:47 AM
SSCertifiable
Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075,
Visits: 4,831
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.
Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1
&
Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger
Post #1408330
Ed Wagner
Ed Wagner
Posted Thursday, January 17, 2013 7:03 AM
SSCommitted
Group: General Forum Members
Last Login: Yesterday @ 1:21 PM
Points: 1,871,
Visits: 541
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'
Post #1408404
anthony.green
anthony.green
Posted Thursday, January 17, 2013 7:08 AM
SSCertifiable
Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075,
Visits: 4,831
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
http://en.wikipedia.org/wiki/ISO_8601
Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1
&
Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger
Post #1408408
dbman
dbman
Posted Thursday, January 17, 2013 7:13 AM
SSC Journeyman
Group: General Forum Members
Last Login: Today @ 3:39 AM
Points: 75,
Visits: 1,080
Thanks Anthony Green. That worked fine.
Post #1408411
anthony.green
anthony.green
Posted Thursday, January 17, 2013 7:15 AM
SSCertifiable
Group: General Forum Members
Last Login: Friday, April 12, 2013 3:51 AM
Points: 5,075,
Visits: 4,831
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.
Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1
&
Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger
Post #1408412
wolfkillj
wolfkillj
Posted Friday, January 18, 2013 9:29 AM
Right there with Babe
Group: General Forum Members
Last Login: Monday, May 20, 2013 6:54 PM
Points: 721,
Visits: 1,375
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
http://en.wikipedia.org/wiki/ISO_8601
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.
Post #1408973
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.