October 31, 2011 at 3:33 am
Hi All,
I need a help on date Diff Concept..
I want to get the Month Number of Given two dates
Ex: 02-Feb-2011 to 25-Feb - 2011
Result: 2
( 2 , Because, Feb Is second Month)
October 31, 2011 at 3:40 am
farooq.hbs (10/31/2011)
Hi All,I need a help on date Diff Concept..
I want to get the Month Number of Given two dates
Ex: 02-Feb-2011 to 25-Feb - 2011
Result: 2
( 2 , Because, Feb Is second Month)
Need you to be a bit clearer. What if your two dates are different months, what would you want to be returned then?
For now, this might help
DECLARE @date DATETIME
SET @date = '02-Feb-2011'
SELECT DATEPART(mm,@date)
October 31, 2011 at 3:59 am
Thnks for u r Reply
Now if the date is from two Diff Month 02-Feb-2011 to 03 - Mar-2011
how to get this following resuklt
Ans: 2
3
October 31, 2011 at 4:20 am
You're talking about only two dates?
If yes, the following should work for you:
DECLARE @STRING VARCHAR(500),
@DATE1 DATETIME,
@DATE2 DATETIME;
SELECT @DATE1 = '20110203',
@DATE2 = '20110405';
SELECT @STRING = ISNULL(CAST(MONTH(@DATE1) AS VARCHAR(2)) + ',', ''),
@STRING = @STRING + CAST(MONTH(@DATE2) AS VARCHAR(2));
SELECT @STRING;
Or do you mean all possible months between two dates?
Best regards,
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
October 31, 2011 at 4:28 am
Hi codebyo,
Thanks for ur Posting..I got My result
October 31, 2011 at 4:33 am
No problem. 🙂
If you need all possible months between two dates you could use something like this:
DECLARE @STRING VARCHAR(500),
@DATE1 DATETIME,
@DATE2 DATETIME,
@MONTHDIFF INT;
SELECT @DATE1 = '20110203',
@DATE2 = '20110905';
SET @MONTHDIFF = MONTH(@DATE1);
WHILE @MONTHDIFF <= MONTH(@DATE2)
BEGIN
SELECT @STRING = ISNULL(@STRING + ',', '') + CAST(@MONTHDIFF AS VARCHAR(2)),
@MONTHDIFF = @MONTHDIFF + 1;
END;
SELECT @STRING;
Best regards,
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
October 31, 2011 at 7:17 am
codebyo (10/31/2011)
No problem. 🙂If you need all possible months between two dates you could use something like this:
DECLARE @STRING VARCHAR(500),
@DATE1 DATETIME,
@DATE2 DATETIME,
@MONTHDIFF INT;
SELECT @DATE1 = '20110203',
@DATE2 = '20110905';
SET @MONTHDIFF = MONTH(@DATE1);
WHILE @MONTHDIFF <= MONTH(@DATE2)
BEGIN
SELECT @STRING = ISNULL(@STRING + ',', '') + CAST(@MONTHDIFF AS VARCHAR(2)),
@MONTHDIFF = @MONTHDIFF + 1;
END;
SELECT @STRING;
Best regards,
No need for a loop here.
DECLARE @DATE1 DATETIME,
@DATE2 DATETIME,
@MONTHDIFF INT;
SELECT @DATE1 = '20110203',
@DATE2 = '20110905';
SELECT STUFF((SELECT ',' + N
FROM (SELECT CONVERT(VARCHAR(2),N) AS N
FROM (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3
UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL
SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9 UNION ALL SELECT 10 UNION ALL
SELECT 11 UNION ALL SELECT 12) tally(N)
WHERE N >= DATEPART(mm,@DATE1) AND N <= DATEPART(mm,@DATE2)
) a
FOR XML PATH(''), TYPE).value('.[1]','varchar(1000)'), 1, 1, '');
October 31, 2011 at 9:15 am
Thank you for the alternative.
I only posted the code that way because we're only dealing with variables and not result sets, so no need to worry about loops there and also because it's easier to read. 😉
Best regards,
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
October 31, 2011 at 10:44 pm
codebyo (10/31/2011)
... so no need to worry about loops there and also because it's easier to read. 😉
You do understand that them's fightin' words, right Andre? 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
November 1, 2011 at 4:06 am
Jeff Moden (10/31/2011)
codebyo (10/31/2011)
... so no need to worry about loops there and also because it's easier to read. 😉You do understand that them's fightin' words, right Andre? 😉
I meant that in comparison with the derived tally table being used in a FOR XML PATH with a string replacement routine. But you got me there. I somewhat feel ashamed of having stated that. :hehe:
Best regards,
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply