SQL Server eliminar milisegundos de la fecha y hora


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

Que esperaría que no me diera ningún resultado... EXCEPTO que estoy obteniendo un registro con una fecha y hora de 2010-07-20 03:21:52.577

¿Cómo puedo hacer que la consulta ignore milisegundos?

Author: marc_s, 2010-07-20

11 answers

Solo tienes que calcular la parte en milisegundos de la fecha y restarla antes de la comparación, así:

select * 
from table 
where DATEADD(ms, -DATEPART(ms, date), date) > '2010-07-20 03:21:52'
 69
Author: Gabe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-20 05:18:31
select * from table
     where DATEADD(ms, DATEDIFF(ms, '20000101', date), '20000101') > '2010-07-20 03:21:52'

Tendrás que recortar milisegundos antes de la comparación, que será lenta en muchas filas

Haga uno de estos para arreglar esto:

  • creó una columna calculada con las expresiones anteriores para comparar con
  • elimine milisegundos al insertar / actualizar para evitar la sobrecarga de lectura
  • Si SQL Server 2008, utilice datetime2(0)
 17
Author: gbn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-20 03:50:41

Intenta:

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

O si su fecha es un valor datetime real:

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

La conversión al estilo 120 corta los milisegundos...

 12
Author: 8kb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-20 04:11:03

Para esta consulta en particular, ¿por qué hacer llamadas a funciones costosas para cada fila cuando solo podría pedir valores que comienzan en el segundo siguiente más alto:

select *
from table
where date >= '2010-07-20 03:21:53'
 8
Author: Harold L,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-07-20 15:48:10

Si está utilizando SQL Server (a partir de 2008), elija uno de estos:

  • CONVERTIR (DATETIME2 (0), su campo de datos)
  • LEFT (RTRIM (CONVERT (DATETIMEOFFSET, YourDateField)), 19)
  • CONVERT (DATETIMEOFFSET (0), YourDateField) with con la adición de un desplazamiento de zona horaria
 7
Author: MMJ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-18 23:07:59

Use CAST con los siguientes parámetros:

Fecha

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

Salida: 2017-10-11

Datetime

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

Salida: 2017-10-11 14:38:50.440

SmallDatetime

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

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

DatetimeOffset

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

Salida: 2017-10-11 14:38:50.4400000 +00:00

Datetime2

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

Salida: 2017-10-11 14:38:50.4400000

 2
Author: Akshay Mishra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-28 05:46:55

Hay más de una manera de hacerlo:

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

O

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

Una llamada a la función menos, pero hay que tener cuidado de desbordar el número entero máximo si las fechas están demasiado separadas.

 1
Author: neves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-10 21:44:16

Por Favor, intente esto

select substring('12:20:19.8470000',1,(CHARINDEX('.','12:20:19.8470000',1)-1))


(No column name)
12:20:19
 1
Author: imbelani nethengwe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-05 09:20:53

Una forma más he configurado consultas SQL Server para ignorar milisegundos cuando estoy buscando eventos de un segundo en particular (en un parámetro en formato "AAAA-MM-DD HH:TT:SS") utilizando un procedimiento almacenado:

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

Podría usar algo similar para ignorar minutos y segundos también.

 0
Author: ftexperts,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-20 16:49:10

Puede ser que esto ayude.. SELECT [Datetime] = CAST ('20120228' COMO smalldatetime)

O / p: 2012-02-28 00:00:00

 0
Author: user3205467,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-01 19:34:44

Utilice el tipo de datos' Smalldatetime '

Seleccione convertir (smalldatetime, getdate ())

Buscará 2015-01-08 15:27:00

 0
Author: Prem Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-08 15:28:44