简体   繁体   中英

SQL:check whether a time column is greater than system time

I would like to know how to check whether a timestamp in a (StartTime Column) is greater than the system time. If greater, it should reset the time stamp into NULL

Is there a way to do that?

Regards.

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

this is for mysql

In MySQL you could try:

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

If you want it to be auto, try this:

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

Update tbl Set
    StartTime=NULL
Where StartTime > @Now
  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();

Try...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM