Solved

First day of current month in sql

Posted on 2013-11-22
11
304 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 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Addition to SQL for dynamic fields 6 38
SqlServer no dupes 25 34
sql server concatenate fields 10 34
VB.NET 2008 - SQL Timeout 9 24
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

770 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