Solved

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

Posted on 2009-06-30
4
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

733 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