Variables para datos de fecha y hora

Escritura de funciones y procedimientos almacenados en SQL Server

Meghan Kwartler

IT Consultant

Taquillas

Escritura de funciones y procedimientos almacenados en SQL Server
-- DECLARE variable y asigna valor inicial
 DECLARE @StartTime as time = '08:00 AM'
-- DECLARE variable y luego SET del valor
DECLARE @StartTime AS time 
SET @StartTime = '08:00 AM'
-- DECLARE variable y luego SET del valor
DECLARE @BeginDate as date 
SET 
  @BeginDate = (
    SELECT TOP 1 PickupDate 
    FROM YellowTripData 
    ORDER BY PickupDate ASC
  );
Escritura de funciones y procedimientos almacenados en SQL Server

Haciendo CAST

-- Sintaxis de CAST
CAST ( expression AS data_type [ ( length ) ] ) 
-- Devuelve expression según data_type
-- DECLARE variable datetime
-- SET valor a @BeginDate y @StartTime usando CAST
DECLARE @StartDateTime as datetime 
SET @StartDateTime = CAST(@BeginDate as datetime) + CAST(@StartTime as datetime)
Escritura de funciones y procedimientos almacenados en SQL Server
-- DECLARE variable de tabla con dos columnas
DECLARE @TaxiRideDates TABLE(
    StartDate date,
    EndDate date)
-- INSERT valores estáticos en la variable de tabla
INSERT INTO @TaxiRideDates (StartDate, EndDate)
SELECT '3/1/2018', '3/2/2018'
-- INSERT resultado de consulta
INSERT INTO @TaxiRideDates(StartDate, EndDate)
SELECT DISTINCT
    CAST(PickupDate  as date), 
    CAST(DropOffDate  as date)
    FROM YellowTripData;
Escritura de funciones y procedimientos almacenados en SQL Server

¡Tu turno: DECLARE, SET, CAST e INSERT!

Escritura de funciones y procedimientos almacenados en SQL Server

Preparing Video For Download...