Este artículo contiene ejemplos de cómo convertir un smalldatetime valor a una fecha y hora valor en SQL Server.
Cuando conviertes un smalldatetime valor a fecha y hora , las horas y los minutos se copian y los segundos y las fracciones de segundo se establecen en 0
.
Ejemplo 1:conversión explícita mediante CAST()
Este es un ejemplo de una conversión explícita. En este caso, uso el CAST()
función directamente dentro del SELECT
declaración para convertir explícitamente de smalldatetime a fechahora .
DECLARE @thesmalldatetime smalldatetime SET @thesmalldatetime = '2031-03-25 11:15:29' SELECT @thesmalldatetime AS 'thesmalldatetime', CAST(@thesmalldatetime AS datetime) AS 'datetime';
Resultado:
+---------------------+-------------------------+ | thesmalldatetime | datetime | |---------------------+-------------------------| | 2031-03-25 11:15:00 | 2031-03-25 11:15:00.000 | +---------------------+-------------------------+
En primer lugar, podemos ver que thesmalldatetime el valor usa 00
para el componente de segundos, aunque lo establecemos explícitamente en 29
. Esto se debe a que al usar thesmalldatetime tipo de dato, los segundos son siempre cero (y sin fracciones de segundo).
Por lo tanto, cuando convertimos el valor a datetime , el valor de los segundos (y los segundos fraccionarios) se establecen en cero. Si acabáramos de asignar el valor original a datetime en primer lugar, los segundos habrían permanecido intactos:
DECLARE @thedatetime datetime SET @thedatetime = '2031-03-25 11:15:29' SELECT @thedatetime AS 'thedatetime';
Resultado:
+-------------------------+ | thedatetime | |-------------------------| | 2031-03-25 11:15:29.000 | +-------------------------+
No solo los segundos permanecerían intactos, sino que también cualquier fracción de segundo habría permanecido intacta (pero solo hasta una escala de 3):
DECLARE @thedatetime datetime SET @thedatetime = '2031-03-25 11:15:29.123' SELECT @thedatetime AS 'thedatetime';
Resultado:
+-------------------------+ | thedatetime | |-------------------------| | 2031-03-25 11:15:29.123 | +-------------------------+
Ejemplo 2:redondeo
Esto es lo que sucede si establecemos el componente de segundos en un valor más alto antes de asignarlo a smalldatetime tipo de datos.
DECLARE @thesmalldatetime smalldatetime SET @thesmalldatetime = '2031-03-25 11:15:31' SELECT @thesmalldatetime AS 'thesmalldatetime', CAST(@thesmalldatetime AS datetime) AS 'datetime';
Resultado:
+---------------------+-------------------------+ | thesmalldatetime | datetime | |---------------------+-------------------------| | 2031-03-25 11:16:00 | 2031-03-25 11:16:00.000 | +---------------------+-------------------------+
Entonces, el componente de minutos ahora se redondea al siguiente minuto.
Ejemplo 3:conversión explícita mediante CONVERT()
Esto es lo mismo que el primer ejemplo, excepto que esta vez uso el CONVERT()
función en lugar de CAST()
.
DECLARE @thesmalldatetime smalldatetime SET @thesmalldatetime = '2031-03-25 11:15:29' SELECT @thesmalldatetime AS 'thesmalldatetime', CONVERT(datetime, @thesmalldatetime) AS 'datetime';
Resultado:
+---------------------+-------------------------+ | thesmalldatetime | datetime | |---------------------+-------------------------| | 2031-03-25 11:15:00 | 2031-03-25 11:15:00.000 | +---------------------+-------------------------+
Ejemplo 4:conversión implícita
Aquí hay un ejemplo de hacer lo mismo, pero usando una conversión de tipo implícita.
DECLARE @thesmalldatetime smalldatetime, @thedatetime datetime SET @thesmalldatetime = '2031-03-25 11:15:29' SET @thedatetime = @thesmalldatetime SELECT @thesmalldatetime AS 'thesmalldatetime', @thedatetime AS 'datetime';
Resultado:
+---------------------+-------------------------+ | thesmalldatetime | datetime | |---------------------+-------------------------| | 2031-03-25 11:15:00 | 2031-03-25 11:15:00.000 | +---------------------+-------------------------+
Entonces obtenemos el mismo resultado, independientemente de si se trata de una conversión explícita o implícita.
Esta es una conversión implícita porque no estamos usando una función de conversión para convertirla explícitamente. Simplemente estamos asignando el valor de una variable de un tipo de datos a una variable de otro tipo de datos. En este caso, SQL Server realiza una conversión implícita en segundo plano cuando intentamos asignar el smalldatetime valor a una fecha y hora variables.