基于当前日期减去两天的SQL Select记录 [英] SQL Select Records based on current date minus two days

查看:146
本文介绍了基于当前日期减去两天的SQL Select记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订单表,其中包含订单ID,订单日期和订单说明.

I have an orders table which contains an order ID, order date and order description.

我想运行一个选择查询,以捕获最近两天创建的所有订单.因此,当前日期减去两天.从12月14日开始,我想选择订单日期大于12月13日的所有订单.这需要使用获取日期"功能来获取当前日期并减去日期.

I want to run a select query which captures all orders that have been created in the last two days. so the current date minus two days. from the 14th December, I would want to select all orders where the order date is > 13th December. This needs to use a Get date function to pick up the current date and minus the days.

我尝试过:

select * from orders where orderdate > getdate() - 2 

,但这不能产生正确的结果.您知道如何执行此操作吗?

but this is not producing the correct results. Any idea's how to do this please?

推荐答案

您应该尝试使用

you should try to use dateadd function

select * from orders where orderdate > dateadd(dd,-1,cast(getdate() as date))

现在,这可能正是您所需要的,但是您需要了解,通过强制转换日期,我们删除了时间部分并有效地返回到一天的开始,而在它之后的一天(-1)则给出了昨天的开始.

Now this may exactly what you need but then you need to understand that by casting to date we remove the time part and effectively going back to start of the day and a day behind it(-1) gives the start of yesterday.

这篇关于基于当前日期减去两天的SQL Select记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆