Solved

First day of current month in sql

Posted on 2013-11-22
11
303 Views
Last Modified: 2013-11-27
Hi Experts,

Can someone tell me how to get the first day of the current month in sql?

Example:

10/31/2013 - CurrentDate
10/01/2013 - First day of the CurrentDate
0
Comment
Question by:daintysally
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 39669760
SELECT DATEADD(mm, DATEDIFF(m, 0, getdate()), 0)
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39669764
something like: SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101) AS FirstDay
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 39669800
(the first day is always the first :)  )

so add (1 - today's day number) to today's date.

select dateadd(day, 1-day(getdate()), getdate()) as date

Open in new window


And for a date, cast it to one:

select cast(dateadd(day, 1-day(getdate()), getdate()) as date)

Open in new window


hth

Mike
0
 

Author Comment

by:daintysally
ID: 39670055
I have tried all of your suggestions and I error on the dateadd function.  I have tried this:
(add_months(getdate(),-1)) and it returns 10/31/2013.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39670081
what is your database engine? all these queries should work on Microsoft SQL Server.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39670084
... which is the topic in which you post your question!
0
 

Author Comment

by:daintysally
ID: 39670098
it's in a tool called Composite,  but all of the script is in sql
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 39670121
Then you're not running SQL Server.

If (add_months(getdate(),-1)) gives the end of last month then what about

add_days(    getdate(),    1 - day(getdate()    )

Mike
0
 

Accepted Solution

by:
daintysally earned 0 total points
ID: 39670162
that's okay, figured it out

date_add(add_months(nameofdatefield,-1),+1)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39675675
dateadd works fine on SQL Server
0
 

Author Closing Comment

by:daintysally
ID: 39680255
Figured solution on my own.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
grouping logic 6 49
Get Duration of last Status Update 4 31
T-SQL:  I Want "Summary"--Not "Detail" 6 22
Sql Query with datetime 3 12
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

920 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