如何从数据库中获取今天日期前 30 天的日期

时间:2021-04-05 13:53:26

标签: sql-server ssms

假设我有一个名为 Products 的表。

<头>
ProductID ProductName ExpDate
1001 可口可乐 2021-6-6
1002 百事可乐 2021-5-8

我想从每个 ExpDate 中获取 Products 并获取该日期之前的 30 天。

例如;第一个 ExpDate 是 2021-6-6,所以减去 30 天 = 2021-5-5

1 个答案:

答案 0 :(得分:1)

您可以使用 DateAdd 函数从日期字段中减去 30 天。

   Select ProductName, ExpDate, dateadd(day,-30,ExpDate) as '30Daysbefore' 
   from Products