2011-08-02 21 views
7

Tengo que actualizar City.Date en la tabla City y tengo columnas en la tabla como Intervalo y Período en la tabla City.enviar datepart como parámetro de una tabla a la función DATEADD en el servidor sql

La columna de intervalo contiene valores como yy, ww, dd, qq, etc. y la columna Período contiene valores como 1,2,3.

Estoy tratando de actualizar City.Date así:

UPDATE City 
SET City.date = DATEADD(City.Interval, City.Period, City.date) 
WHERE CityId = 13 

Se está haciendo error como:

City.Interval no está opción DATEADD reconocido.

¿Cómo puedo actualizar City.Date usando City.Interval, City.Period y City.date?

Respuesta

12

No se puede parametrizar el intervalo de poco

UPDATE City 
SET date = CASE Interval 
       WHEN 'yy' THEN DATEADD(yy, Period, date) 
       WHEN 'ww' THEN DATEADD(ww, Period, date) 
       WHEN 'dd' THEN DATEADD(dd, Period, date) 
       WHEN 'qq' THEN DATEADD(qq, Period, date) 
       WHEN ... 
      END 
WHERE CityId =13 
+0

Gracias GBN. Me funcionó. Buena solución. – Avinash

+0

@Avinash: de nada. Por favor, vote y/o acepte mi respuesta (arriba y la marca de la izquierda). Ver http://meta.stackexchange.com/questions/7237/how-does-reputation-work – gbn

0

Sé que esto es viejo, pero yo siempre he estado haciendo SQL dinámico para hacer la función dateadd aceptar un parámetro. Bueno, hoy hice clic en una ruta diferente, así que hice una función para noquearme.

De esta manera puedo llamarlo como tal

declare @datepart_vc varchar(20) 
set @datepart_vc = 'day' 
select dbo.Dateadd2(@datepart_vc,1,getdate()) 

Función

CREATE FUNCTION dbo.DateAdd2 
(
    -- Add the parameters for the function here 
      @DatePart_VC VARCHAR(20) 
     , @Number_IN INT 
     , @Date_DT DATETIME 
) 
RETURNS DATETIME 
AS 
BEGIN 
    -- Declare the return variable here 
    DECLARE @Return_DT AS DATETIME 

    -- Add the T-SQL statements to compute the return value here 
    SELECT @Return_DT = (
     CASE 
      WHEN @DatePart_VC = 'year' THEN DATEADD(year,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'quarter' THEN DATEADD(quarter,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'month' THEN DATEADD(month,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'dayofyear' THEN DATEADD(dayofyear,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'day' THEN DATEADD(day,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'week' THEN DATEADD(week,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'weekday' THEN DATEADD(weekday,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'hour' THEN DATEADD(hour,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'minute' THEN DATEADD(minute,@Number_IN,@Date_DT) 
      WHEN @DatePart_VC = 'second' THEN DATEADD(second,@Number_IN,@Date_DT) 
      --WHEN @DatePart_VC = 'millisecond' THEN DATEADD(millisecond,@Number_IN,@Date_DT) 
      --WHEN @DatePart_VC = 'microsecond' THEN DATEADD(microsecond,@Number_IN,@Date_DT) 
      --WHEN @DatePart_VC = 'nanosecond' THEN DATEADD(nanosecond,@Number_IN,@Date_DT) 

     END 

    ) 

    -- Return the result of the function 
    RETURN @Return_DT 
Cuestiones relacionadas