如何检索之前的日期并在查询中使用它?

发布于 2024-10-22 03:32:53 字数 387 浏览 1 评论 0原文

我想检索前一个日期(即当前日期减 1)并通过 = 运算符在查询中使用它。

我收到以下查询来检索前一个日期:
select DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1)

它给出: 2011-03-14 00:00:00.000

我想仅获取:2011-03-14,并在查询中使用它来检查列的数据。

类似于:
select * from table where column1 in ('values list') and date=(previous_date)

如何将两者结合起来并以我想要的格式获得它?

I would like to retrieve the previous date (i.e., current date minus 1) and use it in a query through an = operator.

I got the following query to retrieve the previous date:
select DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1)

It gives: 2011-03-14 00:00:00.000

I would like to get only: 2011-03-14, and use this in a query to check against a column's data.

Something like:
select * from table where column1 in ('values list') and date=(previous_date)

How can I combine the two and get it in the format I want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

娇柔作态 2024-10-29 03:32:53

这可能会解决您的问题。

select * from table where 
column1 in ('values list') and 
DATEADD(DD, DATEDIFF(DY, 0, date), 0)=DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1)

This might resolve your issue.

select * from table where 
column1 in ('values list') and 
DATEADD(DD, DATEDIFF(DY, 0, date), 0)=DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1)
不语却知心 2024-10-29 03:32:53

用途:

   DECLARE @Date Datetime;


    SET @Date = DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1);


   select * from table where column1 in ('values list') and Date = @Date

问候

Use :

   DECLARE @Date Datetime;


    SET @Date = DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1);


   select * from table where column1 in ('values list') and Date = @Date

Regards

放手` 2024-10-29 03:32:53
select * from table where column1 in ('values list') 
and CAST(date as DATE)=CAST(DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1) as DATE)
select * from table where column1 in ('values list') 
and CAST(date as DATE)=CAST(DATEADD(DD, DATEDIFF(DY, 0, GETDATE()), -1) as DATE)
山田美奈子 2024-10-29 03:32:53

我得到了我想要的东西:

(SELECT CONVERT(VARCHAR(10),DATEADD(DD, DATEDIFF(DD, 0, GETDATE()), -1),120))

感谢您的帮助!

I got what I was looking for:

(SELECT CONVERT(VARCHAR(10),DATEADD(DD, DATEDIFF(DD, 0, GETDATE()), -1),120))

Thanks for your help!

独自唱情﹋歌 2024-10-29 03:32:53
declare @date varchar(10);
SELECT @date=(CONVERT(VARCHAR(10),DATEADD(DD,-1,GETDATE()),120))
select * from EMPOFFADDRESS where convert(varchar(10),entry`enter code here`date,120) = @date
declare @date varchar(10);
SELECT @date=(CONVERT(VARCHAR(10),DATEADD(DD,-1,GETDATE()),120))
select * from EMPOFFADDRESS where convert(varchar(10),entry`enter code here`date,120) = @date
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文