Solved

SQL- Start of month date function

Posted on 2008-06-20
6
3,618 Views
Last Modified: 2010-01-04
i am currently using this to get the start of the day,
CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME)

what would i use to get the start of the month, also could you explain what the expression means, thank you
0
Comment
Question by:meteorelec
[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
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:contactkarthi
ID: 21832666
how about this one
 
CREATE FUNCTION [dbo].[ufn_GetFirstDayOfMonth] ( @pInputDate    DATETIME )
RETURNS DATETIME
BEGIN 
    RETURN CAST(CAST(YEAR(@pInputDate) AS VARCHAR(4)) + '/' + 
                CAST(MONTH(@pInputDate) AS VARCHAR(2)) + '/01' AS DATETIME) 
END
GO

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21832673
Hello meteorelec,

To get today, I like this better:

CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))

1st of month:

CONVERT(datetime, CONVERT(varchar, DATEADD(d, 1 - DAY(GETDATE())), 101)

Regards,

Patrick
0
 
LVL 2

Author Comment

by:meteorelec
ID: 21832807
Hi Patrick,

CONVERT(datetime, CONVERT(varchar, DATEADD(d, 1 - DAY(GETDATE())), 101)

is give me back

The dateadd function requires 3 argument(s)
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 21833106
Sorry!

CONVERT(datetime, CONVERT(varchar, DATEADD(d, 1 - DAY(GETDATE()), GETDATE()), 101)
0
 
LVL 2

Author Comment

by:meteorelec
ID: 21833149
Incorrect syntax near ')'.

is what i'm getting now
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 21833257
CONVERT(datetime, CONVERT(varchar, DATEADD(d, 1 - DAY(GETDATE()), GETDATE()), 102))
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

724 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