Solved

First day of current month in sql

Posted on 2013-11-22
11
301 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
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.
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.
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…

705 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

12 Experts available now in Live!

Get 1:1 Help Now