[AccessD] SQL Server Date Time

David McAfee davidmcafee at gmail.com
Mon Jul 19 12:20:41 CDT 2010


CREATE FUNCTION [dbo].[RoundTime] (@Time datetime, @RoundTo float)
RETURNS DATETIME AS
BEGIN
   DECLARE @RoundedTime smalldatetime
   DECLARE @Multiplier float

   SET @Multiplier= 24.0/@RoundTo

   SET @RoundedTime= ROUND(CAST(CAST(CONVERT(VARCHAR, at Time,114) AS
DATETIME) AS FLOAT) * @Multiplier,0)/@Multiplier
   RETURN at RoundedTime
END


--To use the function:
--SELECT dbo.roundtime(GETDATE(),1) '1=rounds to 1 hour, .5 rounds to
nearest half hour ...

On Mon, Jul 19, 2010 at 9:24 AM, Kaup, Chester
<Chester_Kaup at kindermorgan.com> wrote:
> I have a database linked to an SQL server table that has a date time column. I want only the date and the hour portion rounded to the nearest hour. For example
>
> 7/18/2010 1:41:00 AM becomes 7/18/2010 2:00:00 AM
>
> Thanks
>
> Chester Kaup
> Engineering Technician
> Kinder Morgan CO2 Company, LLP
> Office (432) 688-3797
> FAX (432) 688-3799
>
>
> No trees were killed in the sending of this message. However a large number of electrons were terribly inconvenienced.
>
>
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>



More information about the AccessD mailing list