you have two options:
1. Set the date so all dates are midnight. This does not work on GETDATE()
or GETUTCDATE(), so you will have to use a trigger if you want the date to
be:
2007-08-16 00:00:00.000
instead of
2007-08-16 10:49:42.670
It is easier to just insert at the date, but you can do this (in efficient,
but this is off the cuff):
CREATE TRIGGER trgItems
ON Items
FOR INSERT
AS
declare @date datetime, @workingdate datetime
set @workingdate = getdate()
-- Get Trimmed date
set @date = CAST((cast(year(@workingdate) as char(4)) + '-'
+ cast(month(@workingdate) as char(2))
+ '-' + cast(day(@workingdate) as char(2))
+ ' 00:00:00.000') as datetime)
UPDATE Items
SET Date_Created = @date
WHERE ItemID IN (SELECT ItemID FROM INSERTED)
Or, you can insert the trimmed date. There are other ways to trim, but I
have been using CAST and CONVERT lately, so i am a bit cast heavy.
The question here is whether to store the date like this, of course.
2. Use a range. But, this will not work in a query like you want.
Here is a solution (not optimized, however)
INSERT INTO #Temp
SELECT CAST(getdate() as varchar(11)) as Date_Created FROM Items
OR
SELECT CAST((cast(year(@workingdate) as char(4)) + '-'
+ cast(month(@workingdate) as char(2))
+ '-' + cast(day(@workingdate) as char(2))
+ ' 00:00:00.000') as datetime) as Date_Created
FROM Items
SELECT Date_Created, COUNT(*) FROM #Temp
DROP TABLE #Temp
This has to be either semi-colon separeted (dangerous for newbies) or placed
in a stored procedure (learning curve).
I would do a search for truncating a date, as I am certain there are more
efficient ways. Note that you can also create this function in .NET if you
are using SQL Server 2005, so you can make this highly efficient.
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! |
*************************************************