dateadd使用datepart from column

时间:2014-04-05 14:53:31

标签: sql-server tsql

是否可以使用DATEADD函数并从每列的列中获取datepart参数?这是一个例子 -

declare @maintenance table
(
  UserID int,
  PropertyID int,
  TaskID int,
  Interval int,
  IntervalUOM varchar(10),
  DateCompleted date,
  primary key (UserID, PropertyID, TaskID, DateCompleted)
);

insert into @maintenance (UserID, PropertyID, TaskID, Interval, IntervalUOM, DateCompleted) values
(2, 1, 4, 6, 'months', '9/30/2010'),
(2, 1, 4, 6, 'months', '4/16/2011'),
(2, 1, 4, 6, 'months', '9/28/2011'),
(2, 1, 4, 6, 'months', '4/10/2012'),
(2, 1, 7, 1, 'years', '12/20/2001'),
(2, 1, 7, 1, 'years', '12/21/2002'),
(2, 1, 7, 1, 'years', '12/16/2003'),
(2, 1, 7, 1, 'years', '12/22/2004'),
(2, 1, 7, 1, 'years', '12/27/2005'),
(2, 1, 7, 1, 'years', '12/31/2006');

select *, NextService = null from @maintenance;
go

目标是填写名为NextService的列 - 例如,第1行需要6个月,并将其添加到DateCompleted(9/30/2010)中显示的日期: enter image description here

-----后来加了...... 发布的答案有效;但是,我想知道是否有办法将IntervalUOM中的值直接转换为datepart参数。这样只需要一条DATEADD线。

2 个答案:

答案 0 :(得分:1)

这是你正在寻找的吗?

SELECT 
    CASE [IntervalUOM] 
        WHEN 'Months' THEN 
            DATEADD(MONTH, Interval, [DateCompleted]) 
        WHEN 'Years' THEN 
            DATEADD(YEAR, Interval, [DateCompleted]) 
    END

这是整个查询:

DECLARE @maintenance TABLE
(
    UserID int,
    PropertyID int,
    TaskID int,
    Interval int,
    IntervalUOM varchar(10),
    DateCompleted date,
    NextService date,
    PRIMARY KEY (UserID, PropertyID, TaskID, DateCompleted)
);

INSERT INTO 
    @maintenance (
        UserID, 
        PropertyID, 
        TaskID, 
        Interval, 
        IntervalUOM, 
        DateCompleted, 
        NextService) 
VALUES 
    (2, 1, 4, 6, 'months', '9/30/2010', NULL),
    (2, 1, 4, 6, 'months', '4/16/2011', NULL),
    (2, 1, 4, 6, 'months', '9/28/2011', NULL),
    (2, 1, 4, 6, 'months', '4/10/2012', NULL),
    (2, 1, 7, 1, 'years', '12/20/2001', NULL),
    (2, 1, 7, 1, 'years', '12/21/2002', NULL),
    (2, 1, 7, 1, 'years', '12/16/2003', NULL),
    (2, 1, 7, 1, 'years', '12/22/2004', NULL),
    (2, 1, 7, 1, 'years', '12/27/2005', NULL),
    (2, 1, 7, 1, 'years', '12/31/2006', NULL);

UPDATE 
    @maintenance 
SET 
    NextService = CASE [IntervalUOM] 
        WHEN 'Months' THEN 
            DATEADD(MONTH, Interval, [DateCompleted]) 
        WHEN 'Years' THEN 
            DATEADD(YEAR, Interval, [DateCompleted]) END
GO

答案 1 :(得分:0)

select这样的查询:

SELECT *, 
  CASE [IntervalUOM] 
    WHEN 'months' THEN DATEADD(MONTH, Interval, [DateCompleted]) 
    WHEN 'years' THEN DATEADD(YEAR, Interval, [DateCompleted]) 
  END AS NextService 
FROM @maintenance;

会得到以下结果:

UserID   PropertyID  TaskID    Interval    IntervalUOM DateCompleted NextService
-------- ----------- --------- ----------- ----------- ------------- -----------
2        1           4         6           months      2010-09-30    2011-03-30
2        1           4         6           months      2011-04-16    2011-10-16
2        1           4         6           months      2011-09-28    2012-03-28
2        1           4         6           months      2012-04-10    2012-10-10
2        1           7         1           years       2001-12-20    2002-12-20
2        1           7         1           years       2002-12-21    2003-12-21
2        1           7         1           years       2003-12-16    2004-12-16
2        1           7         1           years       2004-12-22    2005-12-22
2        1           7         1           years       2005-12-27    2006-12-27
2        1           7         1           years       2006-12-31    2007-12-31

(10 row(s) affected)

要使用它来更新@maintenance表,请执行以下操作:

UPDATE @maintenance
SET NextService = 
  CASE [IntervalUOM] 
    WHEN 'months' THEN DATEADD(MONTH, Interval, [DateCompleted]) 
    WHEN 'years' THEN DATEADD(YEAR, Interval, [DateCompleted]) 
  END 

这假设该表有一个名为NextService的列。