|
|
|
This function takes a date and a date part parameter and rounds the date down to the nearest date part.
Credit goes to "Gordon Klundt"
Example: In order to round '2010-11-03 17:44:10.117' to the nearest hour ('2010-11-03 17:00:00.000') |
Click here to copy the following block | create function dbo.DATEFLOOR ( @seed datetime , @part varchar(2) ) returns datetime as begin
declare @second datetime declare @minute datetime declare @hour datetime declare @day datetime declare @month datetime declare @year datetime declare @retDate datetime
select @second = dateadd(ms,-1*datepart(ms,@seed), @seed) select @minute = dateadd(ss,-1*datepart(ss,@second), @second) select @hour = dateadd(mi,-1*datepart(mi,@minute), @minute) select @day = dateadd(hh,-1*datepart(hh,@hour), @hour) select @month = dateadd(dd,-1*(day(@day)-1), @day) select @year = dateadd(mm,-1*(month(@month)-1), @month)
select @retDate = case when @part = 'ss' then @second when @part = 'mi' then @minute when @part = 'hh' then @hour when @part = 'dd' then @day when @part = 'mm' then @month when @part = 'yy' then @year end
return @retDate end
go |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|