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 2008 Forums
 Transact-SQL (2008)
 Get first date of month

Author  Topic 

keyursoni85
Posting Yak Master

233 Posts

Posted - 2012-07-11 : 07:38:53
First day in previous month, e.g. today July 10, this value default June 1st

First day in this month, e.g. today July 10, this value default July 1st

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-11 : 07:44:03
quote:
Originally posted by keyursoni85

First day in previous month, e.g. today July 10, this value default June 1st

First day in this month, e.g. today July 10, this value default July 1st



SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0);
SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0);

Go to Top of Page

keyursoni85
Posting Yak Master

233 Posts

Posted - 2012-07-11 : 07:45:17
Great thanks.
I am little confused about 1350 value for inner one DATEDIFF(mm,0,GETDATE()).

can you please explain?
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-11 : 07:48:38
It's the number of month boundaries from 1 jan 1900 (0 as a date)
Dateadd that to 0 and you get the first of the current month.

You could also use convert(varchar(6),getdate()) + 01
It's more obvious what is happening if you aren't used to the dateadd but is less efficient.
Probably better for a single date but not if you have a large resultset to work on.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

keyursoni85
Posting Yak Master

233 Posts

Posted - 2012-07-11 : 08:36:33
Ok..thanks
Go to Top of Page
   

- Advertisement -