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
 General SQL Server Forums
 New to SQL Server Programming
 yyyy--mm--dd

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-05-06 : 03:29:03
I would like to get previous first and last month yyyy-mm-dd

I tried this :

select CONVERT(varchar,dateadd(hour,8,dateadd(d,-(day(dateadd(m,-1,getdate()-2))),dateadd(m,-1,getdate()-1))),121)
select CONVERT(varchar,DATEADD(hour,8,dateadd(d,-(day(getdate())),getdate())),121)

but i get the date as same as now. I have an issue on those with 1st april with earlier time will not be included.

How can i get:
start previous month
last previous month

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2014-05-06 : 04:11:42
select CONVERT(Date,dateadd(hour,8,dateadd(d,-(day(dateadd(m,-1,getdate()-2))),dateadd(m,-1,getdate()-1))),121)
select CONVERT(Date,DATEADD(hour,8,dateadd(d,-(day(getdate())),getdate())),121)


Veera
Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2014-05-06 : 05:18:15
i will get this:

2014-04-30 17:16:57.123

any transaction after the time 17:16:57.123 will not be included..this is the issue im facing..
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-05-06 : 09:33:28
quote:
How can i get:
start previous month
last previous month
I didn't quite get what you meant; I am guessing that you want the first day and last day of last month. If so
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0), DATEADD(mm,DATEDIFF(mm,0,GETDATE()),-1)    
You can use convert function to convert to appropriate format if needed. Use VARCHAR with a length e.g. VARCHAR(10).
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-05-09 : 05:24:27
see
http://visakhm.blogspot.in/2010/01/some-quick-tips-for-date-formating.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-06-17 : 02:20:24
Also refer the methods shown at the end of this post http://beyondrelational.com/modules/2/blogs/70/posts/10899/understanding-datetime-column-part-iii.aspx

Madhivanan

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

- Advertisement -