Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Truncating Date


Truncating Date

Author
Message
Andrew Deren
Andrew Deren
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 9

What is the best/most efficient way to truncate DateTime to it's Date value only?

I have a stored procedure that is receiving DateTime and I only want to insert Date part of it (truncate time to 0:00:00)

I don't see any function for this and the only solution I could come up with is to string concatanation with year,month,day function and cast to date back, but that seems way too unefficient.

Thanks.


Andrew Deren
Andrew Deren
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 9

I found a solution on google:

SELECT DATEADD(dd, DATEDIFF(dd,0,@x), 0)

but that seems odd to do datediff and dateadd to get the result.
I wish there was a built in function for that.


Scott Coleman
Scott Coleman
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2942 Visits: 1416

The solution you have is reasonable, but there are many variations. I usually cast the date to a float, truncate it, and cast back to a date.

SELECT CAST(FLOOR(CAST(@x AS FLOAT)) AS DATETIME)

You would probably have to execute this conversion many times to see a performance difference, if any.

PS You can't use CAST(CAST(@x AS INT) AS DATETIME) because it will round PM times up to the next date.





sushila
sushila
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2565 Visits: 639
use convert and apply a style to get only the date...

select convert(varchar, getdate(), 101)

there're any # of formats that you can apply - check BOL for the one that fits your need best!







**ASCII stupid question, get a stupid ANSI !!!**
Andrew Deren
Andrew Deren
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 9
I would think this would be slower than DateAdd solution as this converts to varchar and back.
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5831 Visits: 11402

Absolutely!

Don't convert datetime to varchar, unless you need result as string for reporting purposes.


Fred Williams-134192
Fred Williams-134192
SSC-Enthusiastic
SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)SSC-Enthusiastic (123 reputation)

Group: General Forum Members
Points: 123 Visits: 113
Converting from datetime to varchar and back would be an awful thing to do for looking at a million rows, but performed once per call to modify a stored procedure parameter will not measurably affect performance (unless you are calling the procedure a million times, and then you have bigger problems
Marshall Smith
Marshall Smith
SSC-Enthusiastic
SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)SSC-Enthusiastic (149 reputation)

Group: General Forum Members
Points: 149 Visits: 11
Just out of curiosity, WHY do you not want to store the time? I mean, storing a 0 time doesn't save you any space. It's easy enough to use the DATEPART functions when comparing the values, to ignore the time portion. I might see some advantage to indexing, but that's about it.
Stewart Joslyn
Stewart Joslyn
Old Hand
Old Hand (345 reputation)Old Hand (345 reputation)Old Hand (345 reputation)Old Hand (345 reputation)Old Hand (345 reputation)Old Hand (345 reputation)Old Hand (345 reputation)Old Hand (345 reputation)

Group: General Forum Members
Points: 345 Visits: 188
This comes up regularly. Ive tested most scenarios in the past and datediff/dateadd always comes out the fastest - often by a good margin. It also has the advantage of being locale-independent (doesnt matter whether your dates are ddmmyyy or mmddyyyy or whatever).



Kevin MacCallum
Kevin MacCallum
Old Hand
Old Hand (351 reputation)Old Hand (351 reputation)Old Hand (351 reputation)Old Hand (351 reputation)Old Hand (351 reputation)Old Hand (351 reputation)Old Hand (351 reputation)Old Hand (351 reputation)

Group: General Forum Members
Points: 351 Visits: 1827

Here a script you can use to test the 3 methods.

There is a measureable difference between the convert to varchar and the other 2 options.

Only when you increase the number of iterations > 30,000 do you start to see the speed improvement of the last one

(dateadd(datediff)) but as others have said for a small number of conversions any will work - as long as you take into account the locale specific needs for the varchar option.

Kevin

*****************************

declare @loop int, @d datetime
declare @lMax int
set @lMax = 3000
declare @dStart datetime, @dEnd datetime

--various methods to strip off the time portion off a date

set nocount on
set @loop = 1
set @dStart = getdate()
--uses convert
while @loop <@lMax
begin
set @d= CONVERT(datetime, CONVERT(varchar, getdate(), 101))
set @loop = @loop + 1
end
select datediff(ms, @dStart,getdate())


set @loop = 1
set @dStart = getdate()
--uses cast
while @loop <@lMax
begin
set @d = cast(floor(cast(getdate() as float)) as datetime)
set @loop = @loop + 1
end
select datediff(ms, @dStart,getdate())

set @loop = 1
set @dStart = getdate()
--uses datediff, dateadd
while @loop <@lMax
begin
set @d= dateadd(d, 0, datediff(d, 0, getdate()))
set @loop = @loop + 1
end
select datediff(ms, @dStart,getdate())

set nocount off





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