Solved

First day of current month in sql

Posted on 2013-11-22
11
307 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
[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
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 35

Expert Comment

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

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
Independent Software Vendors: 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!

 

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 70

Expert Comment

by:Éric Moreau
ID: 39670081
what is your database engine? all these queries should work on Microsoft SQL Server.
0
 
LVL 70

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

726 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