SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


What is this saying ?


What is this saying ?

Author
Message
steve4134
steve4134
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 23
I believe this is saying Last day of previous month and first day of current month ? Can someone please confirm ?

Steve



DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101))

and

DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),DATEADD(mm,1,GETDATE())),101))
J Livingston SQL
J Livingston SQL
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6753 Visits: 35982
steve4134 (4/29/2013)
I believe this is saying Last day of previous month and first day of current month ? Can someone please confirm ?

Steve



DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101))

and

DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),DATEADD(mm,1,GETDATE())),101))


run this in SSMS



select DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101))
select DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),DATEADD(mm,1,GETDATE())),101))




________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

J Livingston SQL
J Livingston SQL
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6753 Visits: 35982
good link here

http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50772 Visits: 38657
From the original post:



select
GETDATE() CurrentDate,
DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GETDATE())-1),GETDATE()),101)) FirstOfPreviousMonth,
DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,GETDATE()))),DATEADD(mm,1,GETDATE())),101)) [LastOfPreviousMonth-NOT]




Results:



CurrentDate FirstOfPreviousMonth LastOfPreviousMonth-NOT
----------------------- ----------------------- -----------------------
2013-04-29 12:58:22.217 2013-03-01 00:00:00.000 2013-03-30 00:00:00.000



What you probably want:



select
getdate() CurrentDate,
dateadd(month, datediff(month, 0, getdate()) - 1, 0) FirstOfPreviousMonth,
dateadd(month, datediff(month, 0, getdate()), -1) LastOfPreviousMonth




Results:



CurrentDate FirstOfPreviousMonth LastOfPreviousMonth
----------------------- ----------------------- -----------------------
2013-04-29 12:58:22.217 2013-03-01 00:00:00.000 2013-03-31 00:00:00.000


And if you are generating dates to bracket a month for a WHERE clause I would use the following:



select
getdate() CurrentDate,
dateadd(month, datediff(month, 0, getdate()) - 1, 0) FirstOfPreviousMonth,
dateadd(month, datediff(month, 0, getdate()), 0) FirstOfCurrentMonth




And use it this way:



...
WHERE
MyDateCol >= FirstOfPreviousMonth and
MyDateCol < FirstOfCurrentMonth




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
steve4134
steve4134
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 23
I can not express the joy that comes to me when I see a community come out and help someone so quick. I thank you again. I was correct in my assumptions last month first start date to last date of last month. Thanks again

steve
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50772 Visits: 38657
steve4134 (4/29/2013)
I can not express the joy that comes to me when I see a community come out and help someone so quick. I thank you again. I was correct in my assumptions last month first start date to last date of last month. Thanks again

steve


You did look at the results of the code you posted, right? What you posted returned 3/1/2013 and 3/30/2013 using today (4/29/2013). Not the first and last day of the previous month.

If that is what you need, look at the code I provided.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
steve4134
steve4134
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 23
Yep Lynn

I just wanted to confirm that what I had wrote was indeed from last month threw the end of the month ran today. So if I ran this in May it would be April 1st to last day of April.

Thanks again

steve
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50772 Visits: 38657
steve4134 (4/29/2013)
Yep Lynn

I just wanted to confirm that what I had wrote was indeed from last month threw the end of the month ran today. So if I ran this in May it would be April 1st to last day of April.

Thanks again

steve


Just to be sure you actually understand, the code you posted will NOT always give you the last day of the previous month.

If you don't believe me, try it yourself with various dates using the following code changing the value for the variable @ThisDate:



declare @ThisDate datetime;
set @ThisDate = '20130430 14:25';

select
@ThisDate CurrentDate,
DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@ThisDate)-1),@ThisDate),101)) FirstOfPreviousMonth,
DATEADD(month, -1,CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@ThisDate))),DATEADD(mm,1,@ThisDate)),101)) [LastOfPreviousMonth-NOT]





Modified for SQL Server 2005.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50772 Visits: 38657
Also, if the column(s) you are testing the dates against have time values other than 00:00:00.000, you will miss data at the end of the month.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search