当前位置: 首页 > 面试题库 >

SQL Select Records based on current date minus two days

越骏俊
2023-03-14
问题内容

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

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.

I have tried:

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))

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.



 类似资料:

相关阅读

相关文章

相关问答