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

Get date not working Expand / Collapse
Author
Message
Posted Thursday, January 17, 2013 4:40 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Saturday, September 6, 2014 1:45 PM
Points: 112, Visits: 1,481
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
Posted Thursday, January 17, 2013 4:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Posted Thursday, January 17, 2013 4:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:20 AM
Points: 5,231, Visits: 5,106
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
Posted Thursday, January 17, 2013 7:03 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:22 AM
Points: 4,316, Visits: 3,357
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'




Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1408404
Posted Thursday, January 17, 2013 7:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:20 AM
Points: 5,231, Visits: 5,106
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
Posted Thursday, January 17, 2013 7:13 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Saturday, September 6, 2014 1:45 PM
Points: 112, Visits: 1,481
Thanks Anthony Green. That worked fine.
Post #1408411
Posted Thursday, January 17, 2013 7:15 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 2:20 AM
Points: 5,231, Visits: 5,106
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
Posted Friday, January 18, 2013 9:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:45 AM
Points: 1,222, Visits: 2,546
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.


Jason Wolfkill
Blog: SQLSouth
Twitter: @SQLSouth
Post #1408973
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse