Solved

SQL- Start of month date function

Posted on 2008-06-20
6
3,615 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

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