88
select * from table where date > '2010-07-20 03:21:52' 

which I would expect to not give me any results... EXCEPT I'm getting a record with a datetime of 2010-07-20 03:21:52.577

how can I make the query ignore milliseconds?

1
  • If you post (SQL) code or XML, please highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it! Commented Jul 20, 2010 at 4:55

14 Answers 14

116

You just have to figure out the millisecond part of the date and subtract it out before comparison, like this:

select * from table where DATEADD(ms, -DATEPART(ms, date), date) > '2010-07-20 03:21:52' 
Sign up to request clarification or add additional context in comments.

1 Comment

Please mind that this literally removes milliseconds, it does not remove the smaller units, e.g. For '2021-06-14 00:00:00.1234567' select DATEADD(ms, -DATEPART(ms, date), date) produces 2021-06-14 00:00:00.0004567
56

If you are using SQL Server (starting with 2008), choose one of this:

  • CONVERT(DATETIME2(0), YourDateField)
  • LEFT(RTRIM(CONVERT(DATETIMEOFFSET, YourDateField)), 19)
  • CONVERT(DATETIMEOFFSET(0), YourDateField) -- with the addition of a time zone offset

1 Comment

The DATETIME2 option rounds up if > 0.5, the LEFT(RTRIM option truncates. Both are useful
23

Try:

SELECT * FROM table WHERE datetime > CONVERT(DATETIME, CONVERT(VARCHAR(20), CONVERT(DATETIME, '2010-07-20 03:21:52'), 120)) 

Or if your date is an actual datetime value:

DECLARE @date DATETIME SET @date = GETDATE() SELECT CONVERT(DATETIME, CONVERT(VARCHAR(20), @date, 120)) 

The conversion to style 120 cuts off the milliseconds...

2 Comments

CONVERT(DATETIME, CONVERT(VARCHAR(20), @date, 120)) is a great solution for comparing DateTime values while ignoring milliseconds. Thanks.
VARCHAR(20) was throwing a conversion error for me but I found using VARCHAR(19) instead did the trick
19
select * from table where DATEADD(ms, DATEDIFF(ms, '20000101', date), '20000101') > '2010-07-20 03:21:52' 

You'll have to trim milliseconds before comparison, which will be slow over many rows

Do one of these to fix this:

  • created a computed column with the expressions above to compare against
  • remove milliseconds on insert/update to avoid the read overhead
  • If SQL Server 2008, use datetime2(0)

4 Comments

datetime2 is the best (least expensive) solution IMO
@vacip: in 2010 there would have been less SQL Server 2008+ and more SQL Server 2005 which did not have datetime2
In my experience datetime2(0) will round up the "2020-07-01 15:16:39.837" to "2020-07-01 15:16:40", so is not the best option.
Please note that casting to datetime2(0) will round, not trim.
13

Use CAST with following parameters:

Date

select Cast('2017-10-11 14:38:50.540' as date) 

Output: 2017-10-11

Datetime

select Cast('2017-10-11 14:38:50.540' as datetime) 

Output: 2017-10-11 14:38:50.540

SmallDatetime

select Cast('2017-10-11 14:38:50.540' as smalldatetime) 

Output: 2017-10-11 14:39:00

Note this method rounds to whole minutes (so you lose the seconds as well as the milliseconds)

DatetimeOffset

select Cast('2017-10-11 14:38:50.540' as datetimeoffset) 

Output: 2017-10-11 14:38:50.5400000 +00:00

Datetime2

select Cast('2017-10-11 14:38:50.540' as datetime2) 

Output: 2017-10-11 14:38:50.5400000

1 Comment

Good info here (I learnt things) but wondering how do any of these options help the op? All of these keep the result he doesnt want still coming back, or hide it unreliably (as in, potentially omitting results that should come back)
12

For this particular query, why make expensive function calls for each row when you could just ask for values starting at the next higher second:

select * from table where date >= '2010-07-20 03:21:53' 

2 Comments

It is very likely that the date value is not entered manually, but comes from some variable containing the date, which will be inserted in the query. Therefore, you would need to use functions anyway, as it would be necessary to add 1 second to the time used in the WHERE clause, using for example the function DATEADD(second, 1, date). ;)
@MMJ - Adding a second to the value with DATEADD is much preferable to using a function on the column itself as it doesn't prevent index use
3

Use 'Smalldatetime' data type

select convert(smalldatetime, getdate()) 

will fetch

2015-01-08 15:27:00 

1 Comment

this truncates also seconds which is not what asker wants.
2

REMOVE milliseconds: (2008+)

CAST(date AS datetime2(0)) -- Good if you want 2024-01-01 12:00:00.000 to become 2024-01-01 12:00:00 (note this rounds up so .500 would've turned it to 12:00:01) 

IGNORE milliseconds: (2016+)

DATETRUNC(second, date) -- Turns 2024-01-01 12:00:00.500 into 2024-01-01 12:00:00.000 

https://learn.microsoft.com/en-us/sql/t-sql/functions/datetrunc-transact-sql?view=sql-server-ver16#examples

1 Comment

@MartinSmith thanks for the correction. I had admittedly not seen any simple suggestions of datetime2, at least not without a miriad of other instructions/suggestions with it, so I thought I'd be a helping (my sorting is not the default either, which maybe hasn't helped). Personally I think people will arrive here (like me) wanting to omit milliseconds from the datetime (the subject suggests that). DateTrunc will work (2016+) for turning date parts into 0's which the OP may have liked, yes. The op would've been better off appending ".999" to his date string(s) surely to avoid all this fuss! xD
1

There's more than one way to do it:

select 1 where datediff(second, '2010-07-20 03:21:52', '2010-07-20 03:21:52.577') >= 0 

or

select * from table where datediff(second, '2010-07-20 03:21:52', date) >= 0 

one less function call, but you have to be beware of overflowing the max integer if the dates are too far apart.

Comments

0

One more way I've set up SQL Server queries to ignore milliseconds when I'm looking for events from a particular second (in a parameter in "YYYY-MM-DD HH:TT:SS" format) using a stored procedure:

 WHERE ...[Time_stamp] >= CAST(CONCAT(@YYYYMMDDHHTTSS,'.000') as DateTime) AND ...[Time_stamp] <= CAST(CONCAT(@YYYYMMDDHHTTSS,'.999') as DateTime) 

You could use something similar to ignore minutes and seconds too.

Comments

0

select CONVERT(datetime, FORMAT(yourdate, 'M/d/yyyy h:m:s'))

Comments

-1

Please try this

select substring('12:20:19.8470000',1,(CHARINDEX('.','12:20:19.8470000',1)-1)) (No column name) 12:20:19 

Comments

-1

Review this example:

declare @now datetimeoffset = sysdatetimeoffset(); select @now; -- 1 select convert(datetimeoffset(0), @now, 120); -- 2 select convert(datetimeoffset, convert(varchar, @now, 120)); 

which yields output like the following:

2021-07-30 09:21:37.7000000 +00:00 -- 1 2021-07-30 09:21:38 +00:00 -- 2 2021-07-30 09:21:37.0000000 +00:00 

Note that for (1), the result is rounded (up in this case), while for (2) it is truncated.

Therefore, if you want to truncate the milliseconds off a date(time)-type value as per the question, you must use:

declare @myDateTimeValue = <date-time-value> select cast(convert(varchar, @myDateValue, 120) as <same-type-as-@myDateTimeValue>); 

Comments

-1

I'm very late but I had the same issue a few days ago. None of the solutions above worked or seemed fit. I just needed a timestamp without milliseconds so I converted to a string using Date_Format and then back to a date with Str_To_Date:

STR_TO_DATE(DATE_FORMAT(your-timestamp-here, '%Y-%m-%d %H:%i:%s'),'%Y-%m-%d %H:%i:%s') 

Its a little messy but works like a charm.

1 Comment

Not valid SQL Server syntax

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.