Solved

SQL- Start of month date function

Posted on 2008-06-20
6
3,617 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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.
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 shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

730 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