Solved

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

Posted on 2009-06-30
4
402 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Link SQL table to Webpage 9 38
C# SQL BULK INSERT CLASS 5 34
Sort by Month and Year - SQL 3 22
Mssql SQL query 14 27
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now