Solved

1st day of the current month and first day of next month

Posted on 2009-06-30
4
405 Views
Last Modified: 2012-05-07
Is there a way of building a string to my query can automatically calculate the first day of the current month and the fisrt day of next month?? something like the below would be great.

select CAST('01/01/' + STR(year(getdate())) AS datetime)
0
Comment
Question by:leap29
  • 3
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24744339
Use date functions:
SELECT DATEADD(mm, 0, DATEDIFF(mm, 0, GETDATE()))

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24744349
There are other constructs, but I usually go with above as I can then adjust to previous or next months by simply changing the amount of months to add in the outer DATEADD.
-- next month on the 1st
SELECT DATEADD(mm, 1, DATEDIFF(mm, 0, GETDATE()))

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 24744383
Sorry, I gave you the wrong one.  That is the one I use when dealing with days.  This is for month.
-- this month on the 1st
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
 
-- next month on the 1st
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+1, 0)

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24744631
you can get date as well as day name also.




SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0) as dt,DATENAME(Dw,DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)) as DayName
 
 
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+1, 0),DATENAME(dw,DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+1, 0)) as dayname

Open in new window

0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question