This UDF will strip off the time portion of a DateTime. It can give you either midnight last night, or midnight tonight.
Lets say you have two datetime values @dateStart and @dateEnd, and you want to select records between these dates (excluding any time portion), then you would do:
SELECT *
FROM MyTable
WHERE MyDateTimeColumn >= dbo.kk_fn_UTIL_DateRound(@dateStart, 0)
AND MyDateTimeColumn < dbo.kk_fn_UTIL_DateRound(@dateEnd, 1)
If you want to display dates, without the time, then do:
SELECT dbo.kk_fn_UTIL_DateRound(MyDateColumn, 0) AS [My Date]
FROM MyTable
--
PRINT 'Create function kk_fn_UTIL_DateRound'
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[kk_fn_UTIL_DateRound]') AND xtype IN (N'FN', N'IF', N'TF'))
DROP FUNCTION dbo.kk_fn_UTIL_DateRound
GO
CREATE FUNCTION dbo.kk_fn_UTIL_DateRound
(
@dtDate datetime, -- Date Value to adjust
@intRound int -- 0=Round down [Midnight last night], 1=Round up [Midnight tonight]
)
RETURNS datetime
/* WITH ENCRYPTION */
AS
/*
* kk_fn_UTIL_DateRound Convert date to midnight tonight
* For a "limit" date of '01-Jan-2000' the test needs to be
* MyColumn < '02-Jan-2000'
* to catch any item with a time during 1st Jan
*
* SELECT dbo.kk_fn_UTIL_DateRound(GetDate(), 0) -- Midnight last night
* SELECT dbo.kk_fn_UTIL_DateRound(GetDate(), 1) -- Midnight tonight
*
* Returns:
*
* datetime
*
* HISTORY:
*
* 28-Jul-2005 KBM Started
*/
BEGIN
SELECT @dtDate = DATEADD(Day, DATEDIFF(Day, 0, @dtDate)+@intRound, 0)
RETURN @dtDate
/** TEST RIG
SELECT '01-Jan-2000', dbo.kk_fn_UTIL_DateRound('01-Jan-2000', 0)
SELECT '01-Jan-2000', dbo.kk_fn_UTIL_DateRound('01-Jan-2000', 1)
SELECT '01-Jan-2000 01:02:03', dbo.kk_fn_UTIL_DateRound('01-Jan-2000 01:02:03', 0)
SELECT '01-Jan-2000 01:02:03', dbo.kk_fn_UTIL_DateRound('01-Jan-2000 01:02:03', 1)
SELECT '28-Feb-2000 01:02:03', dbo.kk_fn_UTIL_DateRound('28-Feb-2000 01:02:03', 0)
SELECT '28-Feb-2000 01:02:03', dbo.kk_fn_UTIL_DateRound('28-Feb-2000 01:02:03', 1)
SELECT '29-Feb-2000 01:02:03', dbo.kk_fn_UTIL_DateRound('29-Feb-2000 01:02:03', 0)
SELECT '29-Feb-2000 01:02:03', dbo.kk_fn_UTIL_DateRound('29-Feb-2000 01:02:03', 1)
**/
--==================== kk_fn_UTIL_DateRound ====================--
END
GO
PRINT 'Create function kk_fn_UTIL_DateRound DONE'
GO
--
Kristen