How to remove the time portion of a datetime value (SQL Server)?
Asked Answered
F

6

86

Here's what I use:

SELECT CAST(FLOOR(CAST(getdate() as FLOAT)) as DATETIME)

I'm thinking there may be a better and more elegant way.

Requirements:

  • It has to be as fast as possible (the less casting, the better).
  • The final result has to be a datetime type, not a string.
Fabric answered 5/8, 2008 at 20:8 Comment(0)
F
119

SQL Server 2008 and up

In SQL Server 2008 and up, of course the fastest way is Convert(date, @date). This can be cast back to a datetime or datetime2 if necessary.

What Is Really Best In SQL Server 2005 and Older?

I've seen inconsistent claims about what's fastest for truncating the time from a date in SQL Server, and some people even said they did testing, but my experience has been different. So let's do some more stringent testing and let everyone have the script so if I make any mistakes people can correct me.

Float Conversions Are Not Accurate

First, I would stay away from converting datetime to float, because it does not convert correctly. You may get away with doing the time-removal thing accurately, but I think it's a bad idea to use it because it implicitly communicates to developers that this is a safe operation and it is not. Take a look:

declare @d datetime;
set @d = '2010-09-12 00:00:00.003';
select Convert(datetime, Convert(float, @d));
-- result: 2010-09-12 00:00:00.000 -- oops

This is not something we should be teaching people in our code or in our examples online.

Also, it is not even the fastest way!

Proof – Performance Testing

If you want to perform some tests yourself to see how the different methods really do stack up, then you'll need this setup script to run the tests farther down:

create table AllDay (Tm datetime NOT NULL CONSTRAINT PK_AllDay PRIMARY KEY CLUSTERED);
declare @d datetime;
set @d = DateDiff(Day, 0, GetDate());
insert AllDay select @d;
while @@ROWCOUNT != 0
   insert AllDay
   select * from (
      select Tm =
         DateAdd(ms, (select Max(DateDiff(ms, @d, Tm)) from AllDay) + 3, Tm)
      from AllDay
   ) X
   where Tm < DateAdd(Day, 1, @d);
exec sp_spaceused AllDay;  -- 25,920,000 rows

Please note that this creates a 427.57 MB table in your database and will take something like 15-30 minutes to run. If your database is small and set to 10% growth it will take longer than if you size big enough first.

Now for the actual performance testing script. Please note that it's purposeful to not return rows back to the client as this is crazy expensive on 26 million rows and would hide the performance differences between the methods.

Performance Results

set statistics time on;
-- (All queries are the same on io: logical reads 54712)
GO
declare
    @dd date,
    @d datetime,
    @di int,
    @df float,
    @dv varchar(10);

-- Round trip back to datetime
select @d = CONVERT(date, Tm) from AllDay; -- CPU time = 21234 ms,  elapsed time = 22301 ms.
select @d = CAST(Tm - 0.50000004 AS int) from AllDay; -- CPU = 23031 ms, elapsed = 24091 ms.
select @d = DATEDIFF(DAY, 0, Tm) from AllDay; -- CPU = 23782 ms, elapsed = 24818 ms.
select @d = FLOOR(CAST(Tm as float)) from AllDay; -- CPU = 36891 ms, elapsed = 38414 ms.
select @d = CONVERT(VARCHAR(8), Tm, 112) from AllDay; -- CPU = 102984 ms, elapsed = 109897 ms.
select @d = CONVERT(CHAR(8), Tm, 112) from AllDay; -- CPU = 103390 ms,  elapsed = 108236 ms.
select @d = CONVERT(VARCHAR(10), Tm, 101) from AllDay; -- CPU = 123375 ms, elapsed = 135179 ms.

-- Only to another type but not back
select @dd = Tm from AllDay; -- CPU time = 19891 ms,  elapsed time = 20937 ms.
select @di = CAST(Tm - 0.50000004 AS int) from AllDay; -- CPU = 21453 ms, elapsed = 23079 ms.
select @di = DATEDIFF(DAY, 0, Tm) from AllDay; -- CPU = 23218 ms, elapsed = 24700 ms
select @df = FLOOR(CAST(Tm as float)) from AllDay; -- CPU = 29312 ms, elapsed = 31101 ms.
select @dv = CONVERT(VARCHAR(8), Tm, 112) from AllDay; -- CPU = 64016 ms, elapsed = 67815 ms.
select @dv = CONVERT(CHAR(8), Tm, 112) from AllDay; -- CPU = 64297 ms,  elapsed = 67987 ms.
select @dv = CONVERT(VARCHAR(10), Tm, 101) from AllDay; -- CPU = 65609 ms, elapsed = 68173 ms.
GO
set statistics time off;

Some Rambling Analysis

Some notes about this. First of all, if just performing a GROUP BY or a comparison, there's no need to convert back to datetime. So you can save some CPU by avoiding that, unless you need the final value for display purposes. You can even GROUP BY the unconverted value and put the conversion only in the SELECT clause:

select Convert(datetime, DateDiff(dd, 0, Tm))
from (select '2010-09-12 00:00:00.003') X (Tm)
group by DateDiff(dd, 0, Tm)

Also, see how the numeric conversions only take slightly more time to convert back to datetime, but the varchar conversion almost doubles? This reveals the portion of the CPU that is devoted to date calculation in the queries. There are parts of the CPU usage that don't involve date calculation, and this appears to be something close to 19875 ms in the above queries. Then the conversion takes some additional amount, so if there are two conversions, that amount is used up approximately twice.

More examination reveals that compared to Convert(, 112), the Convert(, 101) query has some additional CPU expense (since it uses a longer varchar?), because the second conversion back to date doesn't cost as much as the initial conversion to varchar, but with Convert(, 112) it is closer to the same 20000 ms CPU base cost.

Here are those calculations on the CPU time that I used for the above analysis:

     method   round  single   base
-----------  ------  ------  -----
       date   21324   19891  18458
        int   23031   21453  19875
   datediff   23782   23218  22654
      float   36891   29312  21733
varchar-112  102984   64016  25048
varchar-101  123375   65609   7843
  • round is the CPU time for a round trip back to datetime.

  • single is CPU time for a single conversion to the alternate data type (the one that has the side effect of removing the time portion).

  • base is the calculation of subtracting from single the difference between the two invocations: single - (round - single). It's a ballpark figure that assumes the conversion to and from that data type and datetime is approximately the same in either direction. It appears this assumption is not perfect but is close because the values are all close to 20000 ms with only one exception.

One more interesting thing is that the base cost is nearly equal to the single Convert(date) method (which has to be almost 0 cost, as the server can internally extract the integer day portion right out of the first four bytes of the datetime data type).

Conclusion

So what it looks like is that the single-direction varchar conversion method takes about 1.8 μs and the single-direction DateDiff method takes about 0.18 μs. I'm basing this on the most conservative "base CPU" time in my testing of 18458 ms total for 25,920,000 rows, so 23218 ms / 25920000 = 0.18 μs. The apparent 10x improvement seems like a lot, but it is frankly pretty small until you are dealing with hundreds of thousands of rows (617k rows = 1 second savings).

Even given this small absolute improvement, in my opinion, the DateAdd method wins because it is the best combination of performance and clarity. The answer that requires a "magic number" of 0.50000004 is going to bite someone some day (five zeroes or six???), plus it's harder to understand.

Additional Notes

When I get some time I'm going to change 0.50000004 to '12:00:00.003' and see how it does. It is converted to the same datetime value and I find it much easier to remember.

For those interested, the above tests were run on a server where @@Version returns the following:

Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c) 1988-2008 Microsoft Corporation Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)

Foliole answered 12/9, 2010 at 22:57 Comment(12)
+1 What version of SQL Server did you test this on by the way?Vanlandingham
It looks like you have single and round backwards in your table. Also, is there any difference in time if you use char instead of varchar?Siskin
@Siskin thanks, fixed. Char appears to be exactly the same as varchar.Foliole
In Oracle there's select round(sysdate) from dual and we definitely need that in Sql Server.Skaw
@Denis, in Oracle there's the simple trunc() function. If you use round, I think you'll find that time after 12 noon are rounded to the next day instead of the current date.Lithographer
@Rick, thanks! In fact, you opened my eyes as to the meaning of this keyword in the context of date-time.Skaw
+1 great answer, but isn't convert to date work faster than datediff?Columbuscolumbyne
@Roman If you are working with SQL Server 2008 and up, yes, converting to date data type is fastest, as shown in my tests above.Foliole
It'd be interesting to know the opposite: what's the best way to keep just the time part?Dairymaid
Time part as "HH:MM:SS" and as "HH:MM" variants, if that makes a diff?Dairymaid
@Dairymaid Please ask a new question. Feel free to comment with a link.Foliole
@ErikE, done. See #40194219.Dairymaid
Z
30

SQL Server 2008 has a new date data type and this simplifies this problem to:

SELECT CAST(CAST(GETDATE() AS date) AS datetime)
Zither answered 6/8, 2008 at 6:44 Comment(1)
I had mistakenly entered 0218 instead of 2018 as the year and the DATEADD(DATEDIFF()) method to cut the time part throws an exception. When I cast the result back to datetime2 your method works nicely select cast(CAST(convert(datetime2(0), '0218-09-12', 120) AS date) as datetime2)Stearin
Z
17

Itzik Ben-Gan in DATETIME Calculations, Part 1 (SQL Server Magazine, February 2007) shows three methods of performing such a conversion (slowest to fastest; the difference between second and third method is small):

SELECT CAST(CONVERT(char(8), GETDATE(), 112) AS datetime)

SELECT DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)

SELECT CAST(CAST(GETDATE() - 0.50000004 AS int) AS datetime)

Your technique (casting to float) is suggested by a reader in the April issue of the magazine. According to him, it has performance comparable to that of second technique presented above.

Zither answered 6/8, 2008 at 8:6 Comment(6)
In my opinion casting to float is not best. Please see my answerFoliole
@Emtucifor I agree that the 3rd method is very obscure because of the 0.50000004 value, but it's the fastest one and your tests confirm that. Thus, it satisfies the as fast as possible requirement.Zither
@Emtucifor Also, here's what the article I linked says about the 0.50000004 value: Although this expression is short (and efficient, as I'll demonstrate shortly), I have to say that I feel uneasy with it. I'm not sure I can put my finger on exactly why—maybe because it's too technical, and you can't see datetime-related logic in it.Zither
If we're going to use this method, I would prefer SELECT CAST(CAST(GETDATE() - '12:00:00.003' AS int) AS datetime) instead, as it means something to me and is imo much easier to remember.Foliole
This is now fastest in SQL 2008: Convert(date, GetDate()).Foliole
The correct link to "Datetime Calculations, Part 1" article is now itprotoday.com/analytics-and-reporting/…Diadromous
B
12

Your CAST-FLOOR-CAST already seems to be the optimum way, at least on MS SQL Server 2005.

Some other solutions I've seen have a string-conversion, like Select Convert(varchar(11), getdate(),101) in them, which is slower by a factor of 10.

Banks answered 5/8, 2008 at 20:12 Comment(2)
We use the method suggested by Michael Stum in one of our products and it works like a charm.Ladyfinger
This is not the optimum way, by quite a bit. Please see my answer on this same page.Foliole
V
4

Please try:

SELECT CONVERT(VARCHAR(10),[YOUR COLUMN NAME],105) [YOURTABLENAME]
Vacuva answered 29/6, 2013 at 9:49 Comment(0)
L
1

SQL2005: I recommend cast instead of dateadd. For example,

select cast(DATEDIFF(DAY, 0, datetimefield) as datetime)

averagely about 10% faster on my dataset, than

select DATEADD(DAY, DATEDIFF(DAY, 0, datetimefield), 0)

(and casting into smalldatetime was faster still)

Loudmouth answered 5/11, 2014 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.