How to get one month ago from today in SQL Server 2008?

lokaqttq  于 2023-10-15  发布在  Go
关注(0)|答案(6)|浏览(110)

I´m writing a query where i get the last month, but with the time in zeros (if today is 2013-05-21 then i want to get 2013-04-21 00:00:00.000).

So I tried:

select (dateadd(month,datediff(month,(0),getdate())-1,(0)));

But I get the first day of the previous month.

Then I tried:

select dateadd(month, -1, GETDATE());

I get the right day, but I also get the current time (2013-04-21 11:41:31.090), and I want the time in zeros.

So how should my query be in order to get something like: 2013-04-21 00:00:00.000

Thanks in advance.

jbose2ul

jbose2ul1#

In SQL Server 2008 there is the date data type, which has no time attached. You can thus remove the time portion quite easily simply by converting, then performing the DateAdd .

SELECT DateAdd(month, -1, Convert(date, GetDate()));

This will return a date data type. To force it to be datetime again, you can simply add one more Convert :

SELECT Convert(datetime, DateAdd(month, -1, Convert(date, GetDate())));

You may not need the explicit conversion to datetime , though.

Note: "One month ago from today" could be defined in many different ways. The way it works in SQL server is to return the day from the previous month that is the closest to the same day number as the current month. This means that the result of this expression when run on March 31 will be February 28. So, you may not get expected results in certain scenarios if you don't think clearly about the ramifications of this, such as if you performed the one-month calculation multiple times, expecting to get the same day in a different month (such as doing March -> February -> January).

See a live demo at SQL Fiddle

The demo shows the values and resulting data types of each expression.

tyky79it

tyky79it2#

Try like this..

select Cast(Cast(dateadd(month, -1, GETDATE()) as Date) as Datetime);
r6l8ljro

r6l8ljro3#

You can use this , it's pretty simple and worked for me -

SELECT SUM( amount ) AS total FROM expenses WHERE MONTH( date ) = MONTH( curdate() ) -1

lqfhib0f

lqfhib0f4#

To get the previous month start date and end date

DECLARE @StartDate date;
    DECLARE @EndDate date;
    select @StartDate= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
    select @EndDate= DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
d7v8vwbk

d7v8vwbk5#

Here are many common dates you may need to pull with logic

SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)  -- Today Midnight
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),-1) -- Yesterday Midnight
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0))  -- First of Last Month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)) -- Same Day Last Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))   -- Last of Last Month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))  -- First of this month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))   -- Last of this month
SELECT DATEADD(d,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)) -- First of next month
SELECT DATEADD(d,DATEPART(DD,GETDATE()-1),DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))  -- Same Day Next Month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+2,0))  -- Last of next month
SELECT DATEADD(d,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+12,0))  -- Last of prior month one year from now
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(DAY, 13-(@@DATEFIRST + (DATEPART(WEEKDAY,GETDATE()) %7)), GETDATE())),0)  -- Next Friday Midnight
2nbm6dog

2nbm6dog6#

Oneliner:

select cast(cast(dateadd(month, -1, getdate()) as date) as datetime) as MyDate

Output:

MyDate
2023-08-15 00:00:00.000

Explanation:

  1. Use DATEADD to subtract 1 month from current date;
  2. Use CAST to DATE , so it will remove time part;
  3. Use CAST to DATETIME to get the date with zeroed time.

相关问题