October 24, 2008 at 5:01 pm
I am tring to figure out how to use datediff and datepart in a syntax that will count only the records in a table from a specific month and return that count.
October 25, 2008 at 3:01 am
Can you please post some sample data and your desired output.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 25, 2008 at 3:44 pm
Here is some data that I used to insert into a table RentalHistory:
INSERT INTO RentalHistory VALUES (462, '09-22-2008', '09-24-2008', 317, 111, NULL );
INSERT INTO RentalHistory VALUES (463, '09-22-2008', '09-25-2008', 329, 112, NULL );
INSERT INTO RentalHistory VALUES (464, '09-23-2008', '09-26-2008', 310, 112, NULL );
INSERT INTO RentalHistory VALUES (465, '09-24-2008', '09-27-2008', 311, 113, NULL );
INSERT INTO RentalHistory VALUES (466, '09-24-2008', '10-01-2008', 312, 113, NULL );
INSERT INTO RentalHistory VALUES (467, '06-25-2008', '06-27-2008', 313, 113, NULL );
INSERT INTO RentalHistory VALUES (468, '06-25-2008', '06-27-2008', 314, 114, NULL );
INSERT INTO RentalHistory VALUES (469, '06-25-2008', '06-27-2008', 315, 114, NULL );
INSERT INTO RentalHistory VALUES (470, '06-25-2008', '06-27-2008', 316, 115, NULL );
INSERT INTO RentalHistory VALUES (471, '06-25-2008', '06-27-2008', 317, 115, NULL );
INSERT INTO RentalHistory VALUES (472, '06-25-2008', '06-27-2008', 325, 116, NULL );
INSERT INTO RentalHistory VALUES (473, '07-27-2008', '07-29-2008', 319, 116, NULL );
INSERT INTO RentalHistory VALUES (474, '07-27-2008', '07-29-2008', 320, 113, NULL );
INSERT INTO RentalHistory VALUES (475, '07-27-2008', '07-29-2008', 321, 110, NULL );
The result that I am looking for is something like this (were all of 09/2008 is counted):
TotalRentals
------------
5
What I am looking to do is just with DATEDIFF use 'mm' for DATEPART.
October 25, 2008 at 4:16 pm
I think I got what I was looking for with a little more messing around with what I already had. Seems to work like I wanted it to. This is the syntax I had come up with that seems to work.
SELECT COUNT(*) AS TotalRentals FROM RentalHistory
WHERE DATEDIFF(mm, TransactionDate, GETDATE()) = 1
October 25, 2008 at 4:18 pm
Try this...
SELECT COUNT(*)
FROM dbo.RentalHistory
WHERE FromDate >= DATEADD(mm,DATEDIFF(mm,0,'20080901'),0)
AND ToDate < DATEADD(mm,DATEDIFF(mm,0,'20080901')+1,0)
... and there's only 4 items in Sep 2008... 😉
For future reference, take a peek at the link I have in my signature to get better answers quicker.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 25, 2008 at 5:16 pm
Thanks, by the way count again LOL first 5 inserts are for the month of 09.......:D
October 25, 2008 at 6:24 pm
webconcepts (10/25/2008)
Thanks, by the way count again LOL first 5 inserts are for the month of 09.......:D
My fault... didn't copy the first line.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply