SQL:检查时间列是否大于系统时间

时间:2011-04-28 15:36:37

标签: asp.net sql sql-server tsql

我想知道如何检查(StartTime列)中的时间戳是否大于系统时间。如果更大,则应将时间戳重置为NULL

有办法吗?

问候。

5 个答案:

答案 0 :(得分:1)

update table set ts_field = null where ts_field > now()

这是针对mysql的

答案 1 :(得分:1)

在MySQL中你可以尝试:

UPDATE table SET time = NULL
WHERE time>NOW()

如果您想要它是自动的,请尝试:

CREATE TRIGGER upd BEFORE INSERT ON `table` 
FOR EACH ROW BEGIN 
    IF NEW.time>NOW() THEN SET NEW.time=NULL; END IF; 
END;

答案 2 :(得分:1)

Declare @Now datetime = GETDATE()

Update tbl Set
    StartTime=NULL
Where StartTime > @Now

答案 3 :(得分:1)

  create trigger tbl_reset_date
  on tbl
  after insert
  as

  update t
  set time = null
  from tbl t
   join inserted i
     on i.id = t.id
  where i.time > getdate();

答案 4 :(得分:0)

...试

CREATE TRIGGER tr_Product_InsteadOfTrigger 
ON Product
INSTEAD OF INSERT
AS

  IF @@ROWCOUNT > 0 -- limit nested recursion if no work to do
  BEGIN

    SET NOCOUNT ON;

    INSERT INTO Product
    (
      ProductId,
      PTime 
    )
    SELECT 
      i.ProductId, 
      CASE 
        WHEN i.PTime > GETUTCDATE() 
        THEN NULL 
        ELSE i.PTime 
      END
    FROM 
      inserted i

  END -- if work to do

GO