Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 First day of month (a year ago)

Author  Topic 

larus
Starting Member

17 Posts

Posted - 2009-02-20 : 07:44:21
Hello,

How to get a date which was the first day of a month a year ago?
Eg. this query produces 2008-02-20 00:00:00.000, and I want 2008-02-01 00:00:00.000

select CAST(FLOOR(CAST(GetDate() AS FLOAT))AS DATETIME) - 366

What would be the best solution?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-20 : 07:46:51
select dateadd(month,1,dateadd(year,datediff(year,0,getdate())-1,0))


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-20 : 07:47:38
or

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

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-20 : 08:10:51
Also try this

SELECT DATEADD(D,-DAY(dateadd(yy,-1,GETDATE())-1),dateadd(yy,-1,GETDATE()))
Go to Top of Page

larus
Starting Member

17 Posts

Posted - 2009-02-20 : 08:27:23
Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-20 : 09:38:36
[code]select dateadd(mm,datediff(mm,0,dateadd(yy,-1,getdate())),0)[/code]
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-02-20 : 23:39:34
quote:
Originally posted by larus

Thanks!



Welcome
Go to Top of Page
   

- Advertisement -