There are a lot of different schools of thought on how to best trim the time from a date, or get the first day of the month, or the first day of the year, etc. For trimming the time, you can use DATEDIFF(), or CONVERT to CHAR(8) with style 112, or convert to FLOAT, etc. For first day of the month and year there are funky DATEADD/DATEDIFF/DATEPART tricks that nobody in their right mind can memorize.While in SQL Server 2008 we have the DATE data type, which makes trimming time easy...DECLARE @d DATETIME;SET @d = CONVERT(DATE, CURRENT_TIMESTAMP);SELECT @d;...even those of us who have upgraded have not already changed all of our data types (this can be a huge refactoring effort) and we still have to potentially pull DATETIME/SMALLDATETIME data from linked servers, so we can't just change everything to DATE and forget about time components.And besides, it doesn't solve the other little intricacies of rounding to specific dates. For example, Oracle has a function called TRUNC() which by default trims the time but can also accept parameters like 'MONTH' which will return the first day of the month. You can see documentation on Oracle's TRUNC() function here:http://www.techonthenet.com/oracle/functions/trunc_date.php
Category