简体   繁体   English

SQL Server查询以获取两个不同列中两个日期之间的差

[英]SQL Server query to get the difference between two dates in two different columns

I would like to write a SQL Server query which should get the difference between two dates (ie dates are available in two columns named "Start date" and "End date"). 我想编写一个SQL Server查询,该查询应该获取两个日期之间的差额(即,日期在名为“开始日期”和“结束日期”的两列中可用)。 I would like to find the difference between two dates in these two different columns and update in another column as "Expired" if the difference is -1 or below. 我想在这两个不同的列中找到两个日期之间的差异,并且如果差异为-1或以下,则在另一列中将其更新为“过期”。 Most importantly: The time should start from the specified start date and it should check periodically. 最重要的是:时间应该从指定的开始日期开始,并且应该定期检查。

You can use DATEDIFF and case when like this: 您可以像这样使用DATEDIFFcase when

update table set expColumn = case when DATEDIFF(day,start_date,end_date) > 0 
       then 'Expired' end FROM table

Hope this helps. 希望这可以帮助。

To find the differential between two date values you use the DATEDIFF() function. 若要查找两个日期值之间的差异,请使用DATEDIFF()函数。

Also, depending on your requirements, you can set this up in your table as a Computed Column. 另外,根据您的要求,您可以在表中将其设置为“计算列”。 That way any changes to the component columns in the computed column's definition will automatically update the computed column to reflect the new values. 这样,对计算列定义中的组件列的任何更改将自动更新计算列以反映新值。
With the PERSISTED keyword you can also allow the computed column to be indexed on. 使用PERSISTED关键字,您还可以允许对计算列进行索引。 Check the documentation here for more details: https://technet.microsoft.com/en-us/library/ms191250%28v=sql.105%29.aspx 在此处查看文档以了解更多详细信息: https : //technet.microsoft.com/zh-cn/library/ms191250%28v=sql.105%29.aspx

This is based on what I think you are trying to do, it's not exactly clear. 这是基于我认为您正在尝试执行的操作,尚不完全清楚。

I don't think you want to add a column to your table to identify what is expired because that column would be dependant on the "End Date" column as well as the primary key which would violate the 3rd normal form. 我认为您不希望在表中添加一列以标识过期的内容,因为该列将取决于“结束日期”列以及将违反第三范式的主键。 It really shouldn't be needed because you can query out which ones are expired at any time. 确实不需要,因为您可以随时查询出哪些过期。 I can't really think of a scenario where you would need to have a column that indicates expiry. 我真的想不出您需要有一列表示到期的情况的情况。 You can create a query like others mentioned to display (not create) another column that marks the expired rows, or you can simply display only the ones expired, or it might make more sense to move them to a different table. 您可以像其他提到的那样创建查询,以显示(而不创建)标记过期行的另一列,也可以仅显示过期的行,或者将它们移至其他表可能更有意义。

SET IDENTITY_INSERT ExpiredTableName ON 

INSERT INTO ExpiredTableName (Column1, Column2, StartDate, EndDate)
SELECT *
FROM TableName
WHERE DATEDIFF(day, InvoiceDate, '2015-04-20') >0

Identity Insert is for auto-generated keys only. 身份插入仅适用于自动生成的密钥。

You can run your queries at regular time intervals like was already mentioned. 您可以像上面提到的那样,按固定的时间间隔运行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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