Convertir una fecha y hora de SQL Server a un formato de fecha más corto
Tengo una columna datetime
en SQL Server que me da datos como este 10/27/2010 12:57:49 pm
y quiero consultar esta columna, pero solo hacer que SQL Server devuelva el día, mes y año, por ejemplo. 2010 10 27
o algo así.
¿Cuáles son las funciones que debería investigar?
¿Debería intentar convertir a otro tipo de datos de fecha? ¿O simplemente convertirlo en una cadena?
9 answers
Echa un vistazo a CONVERT. El 3er parámetro es el estilo de fecha y hora al que desea convertir.
Por ejemplo
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) -- dd/MM/yyyy format
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
2014-08-06 21:22:14
Prueba esto:
print cast(getdate() as date )
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
2014-02-19 14:29:25
Si necesita el resultado en un formato de fecha, puede usar:
Select Convert(DateTime, Convert(VarChar, GetDate(), 101))
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-11-20 14:46:27
Además de CAST y CONVERT, si está utilizando Sql Server 2008, puede convertir a un tipo de fecha (o usar ese tipo para comenzar), y luego opcionalmente volver a convertir a un varchar:
declare @myDate date
set @myDate = getdate()
print cast(@myDate as varchar(10))
Salida:
2012-01-17
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-01-17 20:27:19
Con SQL Server 2005, usaría esto:
select replace(convert(char(10),getdate(),102),'.',' ')
Resultados: 2015 03 05
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
2015-03-05 19:27:13
El formato de fecha más corto de mm/dd/aa puede obtenerse con:
Select Convert(varchar(8),getdate(),1)
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-05-26 16:14:32
Para cualquiera de las versiones de SQL Server: dateadd(dd, datediff(dd, 0, getdate()), 0)
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
2015-12-17 03:18:30
Si tienes un campo datetime que da los resultados como este 2018-03-30 08:43: 28.177
Propuesto: y desea cambiar el datetime a date para que aparezca como 2018-03-30
cast(YourDateField as Date)
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-06-28 21:34:06
Simplemente agregue la palabra clave date. Por ejemplo, seleccione la fecha (orderdate), cuente (1) desde pedidos donde orderdate > '2014-10-01' agrupe por fecha (orderdate);
Orderdate está en date time. Esta consulta mostrará los pedidos para esa fecha en lugar de datetime.
La palabra clave Date aplicada en una columna datetime la cambiará a short date.
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
2014-11-14 12:17:21