﻿<?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 2005 / T-SQL (SS2K5)  / Converting a date within the WHERE or JOIN areas / 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>Fri, 24 May 2013 11:50:22 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>In 2008, the best way to truncate hours is CONVERT(date, YourDate). Took long enough to get some new time datatypes. hah!Also, since the data warehouse I work with is using day as the granularity, we often need to use something like this:AND CONVERT(date, TransactionDate) = DWDateAlthough if performance is an issue, I guess we could do it like this:AND TransactionDate &amp;gt;= Table1.DimTimeDateAND TransactionDate &amp;lt; Table2.DimTimeDate --(date + 1d)Totally removing any functions would require a second join on the time dimension at DATEADD(dd,1,DWDate).</description><pubDate>Thu, 13 May 2010 09:36:27 GMT</pubDate><dc:creator>amenjonathan</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>There is. Calculated persisted columns can be indexed. Full details in Books Online.</description><pubDate>Tue, 09 Jun 2009 05:14:37 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>Gail,Is there any way to add an index to the function of a column? In Foxpro I am able to do things like:INDEX ON VAL(SeqNum) AS SeqIs there anything analagous in SQL Server?</description><pubDate>Tue, 09 Jun 2009 04:19:31 GMT</pubDate><dc:creator>Allister Reid</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>I've just looked at the data that the query is looking at and the time part of the datetime fields are always zero, so there's no need to do this work to strip the time out - they should have been able to compare the fields without any messing about.I'll have a play with slightly healthier ways of comparing dates where there is a time to consider/remove.Thanks for the assistance Gail, Allister.BrainDonor.</description><pubDate>Tue, 09 Jun 2009 04:09:49 GMT</pubDate><dc:creator>BrainDonor</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>[quote][b]BrainDonor (6/9/2009)[/b][hr]If a selection is required on the date part of a datetime field what would be the best way of doing it?[/quote]BETWEEN's often very useful[quote]Another method I have used is using DATEDIFF to check the number of days difference, but does this suffer the same kind of penalty?[/quote][b]Any[/b] form of function on a column will prevent index seeks on that column. That includes all SQL functions, all forms or arithmetic, string concatenation, etc. If you apply any change to a column in the where/join then it's no longer eligible for index seeks.</description><pubDate>Tue, 09 Jun 2009 03:54:53 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>Thanks Gail. I suspected that, and thought using a CAST and CONVERT wouldn't be too helpfull either.If a selection is required on the date part of a datetime field what would be the best way of doing it?Another method I have used is using DATEDIFF to check the number of days difference, but does this suffer the same kind of penalty?Thanks,BrainDonor</description><pubDate>Tue, 09 Jun 2009 03:44:51 GMT</pubDate><dc:creator>BrainDonor</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>Any form of function on a column will prevent index seeks on that column. So if you use that form in a where/join, you may very well be hindering performance.</description><pubDate>Tue, 09 Jun 2009 03:32:46 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>Yes, I hadn't thought about comparing just the date (without the time) - obvious now that someone else points it out! Still an ugly way of doing it though.</description><pubDate>Tue, 09 Jun 2009 03:12:03 GMT</pubDate><dc:creator>BrainDonor</dc:creator></item><item><title>RE: Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>This is code for getting a date without the time portion.I'm not sure about actual performance, I'd need to test, however, the following keeps the date as a dat, without the (IMO) ugly cast.select dateadd(day, datediff(day, 0, getdate()), 0)</description><pubDate>Tue, 09 Jun 2009 03:07:23 GMT</pubDate><dc:creator>Allister Reid</dc:creator></item><item><title>Converting a date within the WHERE or JOIN areas</title><link>http://www.sqlservercentral.com/Forums/Topic731245-338-1.aspx</link><description>I regularly use such code as:CONVERT(VARCHAR(11),date_field,106) in order to convert a date within a database to a format more suitable for displaying.But I've come across code that does such things within the WHERE clause and the JOIN clause. For example:WHERE CAST(CONVERT(VARCHAR(11),Expiry_Date,106) AS DATETIME) &gt;= CAST(CONVERT(VARCHAR(11),GETDATE(),106) AS DATETIME))I've never bothered converting dates where they haven't been required as output fields for displaying or suchlike.My first thought is that this code is unnecessary and will just slow things down.Am I correct in believing this, or is there a good reason for using such code where the date will never be selected for output?Regards,BrainDonor</description><pubDate>Tue, 09 Jun 2009 02:57:14 GMT</pubDate><dc:creator>BrainDonor</dc:creator></item></channel></rss>