 |
|
 |
|
Next: Problems creating full text index with WSS 2.0 / ..
|
| Author |
Message |
External

Since: Nov 13, 2008 Posts: 49
|
(Msg. 1) Posted: Wed Dec 03, 2008 3:25 am
Post subject: Last day of previous month Archived from groups: microsoft>public>sqlserver>programming (more info?)
|
|
|
Hello,
[SQL2000]
CREATE TABLE [dbo].[tbl_FGSIZE]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Rec_Create_Date] [smalldatetime] NOT NULL,
[SizeMB] [numeric](19, 2) NULL,
[UsedMB] [numeric](19, 2) NULL,
[FreeMB] [numeric](19, 2) NULL,
)
This table is populated by a daily job. I need to write a query that will
find the FreeMB for a particular ID today, as well as the value for last day
of previous month.So ideally if I run it today, the resultset would be:
ID FreeMB Rec_Create_Date Last Month FreeMB
-- --------- ------------------ ------------ --------
102 300 Dec 03, 2008 Nov 30,2008 350
TIA,
AbbA >> Stay informed about: Last day of previous month |
|
| Back to top |
|
 |  |
External

Since: Jan 19, 2008 Posts: 38
|
(Msg. 2) Posted: Wed Dec 03, 2008 4:25 am
Post subject: Re: Last day of previous month [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
You can calculate the last day of the previous month by using DATEADD on the
first of the current month.
A convoluted example, I'm sure there's a quicker way:
use tempdb;
DECLARE @Today datetime;
SET @Today = GETDATE();
DECLARE @FirstOfMonthString nchar(  ;
SET @FirstOfMonthString = CAST(YEAR(@Today) AS NCHAR(4)) + RIGHT('0' +
CAST(MONTH(@Today) AS NVARCHAR(2)), 2) + '01';
DECLARE @FirstOfMonth datetime;
SET @FirstOfMonth = @FirstOfMonthString;
SET @FirstOfMonth = DATEADD(d, -1, @FirstOfMonth);
SELECT @FirstOfMonth FirstOfMonth;
--
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
"Abba" wrote in message
> Hello,
>
> [SQL2000]
>
> CREATE TABLE [dbo].[tbl_FGSIZE]
> (
> [ID] [int] IDENTITY(1,1) NOT NULL,
> [Rec_Create_Date] [smalldatetime] NOT NULL,
> [SizeMB] [numeric](19, 2) NULL,
> [UsedMB] [numeric](19, 2) NULL,
> [FreeMB] [numeric](19, 2) NULL,
> )
>
> This table is populated by a daily job. I need to write a query that will
> find the FreeMB for a particular ID today, as well as the value for last
> day of previous month.So ideally if I run it today, the resultset would
> be:
>
> ID FreeMB Rec_Create_Date Last Month FreeMB
> -- --------- ------------------ ------------ --------
> 102 300 Dec 03, 2008 Nov 30,2008 350
>
>
> TIA,
> AbbA
> >> Stay informed about: Last day of previous month |
|
| Back to top |
|
 |  |
External

Since: Jan 15, 2008 Posts: 380
|
(Msg. 3) Posted: Wed Dec 03, 2008 4:25 am
Post subject: Re: Last day of previous month [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Here's another way:
select dateadd(d, - (datepart(dd, dateadd(d, datediff(d, 0, getdate()),
0))), dateadd(d, datediff(d, 0, getdate()), 0))
ML
---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/ >> Stay informed about: Last day of previous month |
|
| Back to top |
|
 |  |
External

Since: Jan 10, 2008 Posts: 640
|
(Msg. 4) Posted: Wed Dec 03, 2008 6:48 am
Post subject: Re: Last day of previous month [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Dec 3, 8:33 am, Dieter Noeth wrote:
> Plamen Ratchev wrote:
> > Here is another method:
>
> > SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0,
> > CURRENT_TIMESTAMP), 0))
>
> And finally the shortest solution
>
> SELECT DATEADD(DAY, - DATEPART(DAY, date_col), date_col)
>
> Dieter
Dieter,
BRAVO! >> Stay informed about: Last day of previous month |
|
| Back to top |
|
 |  |
External

Since: Aug 20, 2008 Posts: 672
|
(Msg. 5) Posted: Wed Dec 03, 2008 7:43 am
Post subject: Re: Last day of previous month [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Jul 26, 2004 Posts: 18
|
(Msg. 6) Posted: Wed Dec 03, 2008 10:25 am
Post subject: Re: Last day of previous month [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Plamen Ratchev wrote:
> Here is another method:
>
> SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0,
> CURRENT_TIMESTAMP), 0))
And finally the shortest solution
SELECT DATEADD(DAY, - DATEPART(DAY, date_col), date_col)
Dieter >> Stay informed about: Last day of previous month |
|
| Back to top |
|
 |  |
External

Since: Aug 20, 2008 Posts: 672
|
(Msg. 7) Posted: Wed Dec 03, 2008 10:25 am
Post subject: Re: Last day of previous month [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Jan 11, 2008 Posts: 440
|
(Msg. 8) Posted: Wed Dec 03, 2008 7:26 pm
Post subject: Re: Last day of previous month [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On Wed, 03 Dec 2008 07:43:10 -0500, Plamen Ratchev wrote:
>Here is another method:
>
>SELECT DATEADD(DAY, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0,
>CURRENT_TIMESTAMP), 0))
This one is shorter AND does not retain the time part:
SELECT DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), -1);
This one is basically the same but a little easier to understand because
it doesn't rely on integer to datetime conversion:
SELECT DATEADD(month, DATEDIFF(month, '19000101', CURRENT_TIMESTAMP),
'18991231');
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis >> Stay informed about: Last day of previous month |
|
| Back to top |
|
 |  |
| Related Topics: | Get the hiredate which fall in the previous month - Hi, How can I get all the hiredates which is falls in the previous month? Supposed today is November 9th. So I would like to take todays' date - GetDate() Now I want to find the previous month's hires. SELECT FirstName + ' ' + LastName AS..
Creating a month by month lookup table from incomplete data. - Hi, I hope someone has an idea how I might do this. I have a table that shows goods recieved. Simplified it looks something like this: Part Date QTY Vendor..
Substituting a value from a previous row where a value is .. - Hey can anyone help with this? Below is a table that shows some parts against month and a vendor ID number. I would like to put together a statement, that, where the VENDOR_ID is NULL for a month, looks back at the previous month for the same PART to..
Returning Previous/future months across years - I've got a table with a field for Month and a field for Year and an IDENTITY field: create table #t (id int identity, m int, y int) insert #t values(12, 2005) insert #t values(1, 2006) insert #t values(3, 2006) insert #t values(4, 2006) insert #t..
Returning Previous/future months across years - I've got a table with a field for Month and a field for Year and an IDENTITY field: create table #t (id int identity, m int, y int) insert #t values(12, 2005) insert #t values(1, 2006) insert #t values(3, 2006) insert #t values(4, 2006) insert #t.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|