﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / Get date not working / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 10:27:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>[quote][b]anthony.green (1/17/2013)[/b][hr][quote][b]Ed Wagner (1/17/2013)[/b][hr]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 &amp;lt;= and &amp;gt;= 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:[code="sql"]WHERE Invoice_Date BETWEEN '01/01/2013 00:00:00.000'                       AND '01/01/2013 23:59:59.999'[/code][/quote]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 datetime2But always try to remember to qualify your dates as ISO formats so that SQL doesnt get confused or set the dateformat to the correct formatYYYY-MM-DDTHH:MM:SS.msTake 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[url]http://en.wikipedia.org/wiki/ISO_8601[/url][/quote]It would be even better to specify start and end dates using the "half-open interval" model, like this: WHERE DatetimeColumn &amp;gt;= '2013-01-01 00:00:00.000' AND DatetimeColumn &amp;lt; '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.</description><pubDate>Fri, 18 Jan 2013 09:29:51 GMT</pubDate><dc:creator>wolfkillj</dc:creator></item><item><title>RE: Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>[quote][b]ziako (1/17/2013)[/b][hr]Thanks Anthony Green. That worked fine.[/quote]No problem, glad I could assist.May I suggest some further reading [url]http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/[/url]They may help you out with other date related issues you may run into.</description><pubDate>Thu, 17 Jan 2013 07:15:46 GMT</pubDate><dc:creator>anthony.green</dc:creator></item><item><title>RE: Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>Thanks Anthony Green. That worked fine.</description><pubDate>Thu, 17 Jan 2013 07:13:56 GMT</pubDate><dc:creator>dbman</dc:creator></item><item><title>RE: Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>[quote][b]Ed Wagner (1/17/2013)[/b][hr]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 &amp;lt;= and &amp;gt;= 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:[code="sql"]WHERE Invoice_Date BETWEEN '01/01/2013 00:00:00.000'                       AND '01/01/2013 23:59:59.999'[/code][/quote]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 datetime2But always try to remember to qualify your dates as ISO formats so that SQL doesnt get confused or set the dateformat to the correct formatYYYY-MM-DDTHH:MM:SS.msTake 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[url]http://en.wikipedia.org/wiki/ISO_8601[/url]</description><pubDate>Thu, 17 Jan 2013 07:08:34 GMT</pubDate><dc:creator>anthony.green</dc:creator></item><item><title>RE: Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>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 &amp;lt;= and &amp;gt;= 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:[code="sql"]WHERE Invoice_Date BETWEEN '01/01/2013 00:00:00.000'                       AND '01/01/2013 23:59:59.999'[/code]</description><pubDate>Thu, 17 Jan 2013 07:03:03 GMT</pubDate><dc:creator>Ed Wagner</dc:creator></item><item><title>RE: Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>Thats becuase you probably wont have anything that is equal to the date being used in the where clauseYou really need to do an &amp;gt;= &amp;lt; where clauseSELECT [Invoice_Date],[UserId],[Customer],[InvoiceNo],[Description],[Pack],[PIP_Code],[Class],[Major_Grp],[Invoice_Val],[Cost_Pr],[Quantity]FROM [Telesales].[dbo].[Sales]Where Invoice_Date &amp;gt;=  dateadd(dd, datediff(dd, 0, GETDATE()) - 1, 0)AND Invoice_Date &amp;lt; dateadd(dd, datediff(dd, 0, GETDATE()), 0)Remember that dates are forever moving valuesNo two runs of the query are the same when using = and getdate() as the time stamp will change on each run.</description><pubDate>Thu, 17 Jan 2013 04:47:18 GMT</pubDate><dc:creator>anthony.green</dc:creator></item><item><title>RE: Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>it's not work properly............</description><pubDate>Thu, 17 Jan 2013 04:42:01 GMT</pubDate><dc:creator>alpeshchauhan</dc:creator></item><item><title>Get date not working</title><link>http://www.sqlservercentral.com/Forums/Topic1408326-391-1.aspx</link><description>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)</description><pubDate>Thu, 17 Jan 2013 04:40:21 GMT</pubDate><dc:creator>dbman</dc:creator></item></channel></rss>