Solved

SQL- Start of month date function

Posted on 2008-06-20
6
3,612 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now