Solved

First day of current month in sql

Posted on 2013-11-22
11
308 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

729 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