June 9, 2009 at 2:57 am
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) >= 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
June 9, 2009 at 3:07 am
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)
June 9, 2009 at 3:32 am
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.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 9, 2009 at 3:44 am
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
June 9, 2009 at 3:54 am
BrainDonor (6/9/2009)
If a selection is required on the date part of a datetime field what would be the best way of doing it?
BETWEEN's often very useful
Another method I have used is using DATEDIFF to check the number of days difference, but does this suffer the same kind of penalty?
Any 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.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
June 9, 2009 at 4:09 am
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.
June 9, 2009 at 4:19 am
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 Seq
Is there anything analagous in SQL Server?
June 9, 2009 at 5:14 am
There is. Calculated persisted columns can be indexed. Full details in Books Online.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 13, 2010 at 9:36 am
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) = DWDate
Although if performance is an issue, I guess we could do it like this:
AND TransactionDate >= Table1.DimTimeDate
AND TransactionDate < Table2.DimTimeDate --(date + 1d)
Totally removing any functions would require a second join on the time dimension at DATEADD(dd,1,DWDate).
-------------------------------------------------------------------------------------------------
My SQL Server Blog
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply