SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DateFloor Function


DateFloor Function

Author
Message
gklundt
gklundt
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 140
Comments posted to this topic are about the item DateFloor Function
Per Siden
Per Siden
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 94
Won't the query cost for

select dbo.datefloor(getdate(),'dd')

be a lot higher than say

select convert(datetime, convert(char(10), getdate(), 120))

?
gklundt
gklundt
Valued Member
Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)Valued Member (70 reputation)

Group: General Forum Members
Points: 70 Visits: 140
Yes, you will have a scalar computation versus a datatype conversion - slightly more expensive. This was more for load functions where rounding down to the nearest hour or minute was the goal, or trimming miliseconds (as developers will often ask). Cast/convert date to string is probably still recommended for rounding down to the nearest day. This is more of a pattern to round down to other parts. The cost is realized in CPU at load, and the benefit is realized in post-processing when doing aggregations to the sub-day grain without further scalar computations at report time.
Hogan Long
Hogan Long
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 3
This is not the best way to implement this -- See http://stackoverflow.com/questions/85373/floor-a-date-in-sql-server

declare @datetime datetime;
set @datetime = getdate();
select @datetime;
select dateadd(year,datediff(year,0,@datetime),0);
select dateadd(month,datediff(month,0,@datetime),0);
select dateadd(day,datediff(day,0,@datetime),0);
select dateadd(hour,datediff(hour,0,@datetime),0);
select dateadd(minute,datediff(minute,0,@datetime),0);
select dateadd(second,datediff(second,'2000-01-01',@datetime),'2000-01-01');



or converted to this function:

create function dbo.DATEFLOOR (
@seed datetime
, @part varchar(2)
)
returns datetime
as
begin

/*
Sample Usage (uses standard dateparts):
select 'second', dbo.datefloor(getdate(),'ss') union all
select 'minute', dbo.datefloor(getdate(),'mi') union all
select 'hour' , dbo.datefloor(getdate(),'hh') union all
select 'day' , dbo.datefloor(getdate(),'dd') union all
select 'month' , dbo.datefloor(getdate(),'mm') union all
select 'year' , dbo.datefloor(getdate(),'yy')
*/

select @retDate = case
when @part = 'ss' then dateadd(second,datediff(second,'2000-01-01',@seed),'2000-01-01')
when @part = 'mi' then dateadd(minute,datediff(minute,0,@seed),0)
when @part = 'hh' then dateadd(hour,datediff(hour,0,@seed),0)
when @part = 'dd' then dateadd(day,datediff(day,0,@seed),0)
when @part = 'mm' then dateadd(month,datediff(month,0,@seed),0)
when @part = 'yy' then dateadd(year,datediff(year,0,@seed),0)
end

return @retDate
end


Iwas Bornready
Iwas Bornready
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13740 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search