Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

First day of current month in sql

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
daintysally
Asked:
daintysally
  • 4
  • 3
  • 2
  • +1
1 Solution
 
YZlatCommented:
SELECT DATEADD(mm, DATEDIFF(m, 0, getdate()), 0)
0
 
Éric MoreauSenior .Net ConsultantCommented:
something like: SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101) AS FirstDay
0
 
DcpKingCommented:
(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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
daintysallyAuthor Commented:
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
 
Éric MoreauSenior .Net ConsultantCommented:
what is your database engine? all these queries should work on Microsoft SQL Server.
0
 
Éric MoreauSenior .Net ConsultantCommented:
... which is the topic in which you post your question!
0
 
daintysallyAuthor Commented:
it's in a tool called Composite,  but all of the script is in sql
0
 
DcpKingCommented:
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
 
daintysallyAuthor Commented:
that's okay, figured it out

date_add(add_months(nameofdatefield,-1),+1)
0
 
YZlatCommented:
dateadd works fine on SQL Server
0
 
daintysallyAuthor Commented:
Figured solution on my own.
0

Featured Post

Industry Leaders: 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!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now