使日期动态,但保持时间静态

时间:2014-12-12 18:43:44

标签: sql sql-server-2008 tsql

我有这个查询,我必须每天手动更改日期而不是时间。 下面你可以看到我有第二天2014-12-11我必须将日期改为2014-12-12。任何想法如何使这种动态?

UPDATE tblData SET Issued = '2014-12-11 08:00:00.000' 
  where issued < '2014-12-11 08:00:00.000' and called > '2014-12-11 08:00:01.000' 
  and tbldate = '2014-12-11'

4 个答案:

答案 0 :(得分:1)

DECLARE @Date DATE = '2014-12-11'  --<-- Date variable 

UPDATE tblData 
   SET Issued = CAST(@Date AS DATETIME) +  CAST('08:00:00.000' AS DATETIME)
where issued < CAST(@Date AS DATETIME) + CAST('08:00:00.000' AS DATETIME)
  and called > CAST(@Date AS DATETIME) + CAST('08:00:01.000' AS DATETIME)
  and tbldate = @Date

答案 1 :(得分:0)

你能在这里使用DATEADD功能吗?

演示:

DECLARE @TestData TABLE (Issued DATETIME);

INSERT INTO @TestData VALUES(DATEADD(day, -1, GETDATE()));

SELECT * FROM @TestData;

UPDATE @TestData
SET Issued = DATEADD(day, 1, Issued);

SELECT * FROM @TestData;

答案 2 :(得分:0)

为什么每天都会更新数据?只需使用视图或添加计算列即可获得所需内容。它会是这样的:

alter table
    add TodayIssued as dateadd(day,
                               datediff(day, Issued, getdate()),
                               Issued) ;

答案 3 :(得分:0)

UPDATE tblData
SET Issued =  concat(substring(convert(varchar(10),getdate(),105),1,10),' 08:00:00.000')  
where issued <  concat(substring(convert(varchar(10),getdate(),105),1,10),' 08:00:00.000' )
    AND
    called > concat(substring(convert(varchar(10),getdate(),105),1,10),' 08:00:01.000')
    and tbldate = substring(convert(varchar(10),getdate(),105),1,10)