Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select all months bewtween two dates


Select all months bewtween two dates

Author
Message
twin.devil
twin.devil
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1376 Visits: 2596
feroz_tt (4/16/2014)
Hi All,

I'm getting some problem here

;with Numbers (Number) as
(select row_number() over (order by object_id)
from sys.all_objects)
select dateadd(month, number, '2014-09-01')
from Numbers
where number <= datediff(month, '2014-09-01', '2016-03-31')

Low no of months.

Select datediff(month, '2014-09-01', '2016-03-31')
it should give 31 instead of 18.

I don't understand where i'm wrongUnsure


How did you calculate 31?
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45002 Visits: 39880
feroz_tt (4/16/2014)
Hi All,

I'm getting some problem here

;with Numbers (Number) as
(select row_number() over (order by object_id)
from sys.all_objects)
select dateadd(month, number, '2014-09-01')
from Numbers
where number <= datediff(month, '2014-09-01', '2016-03-31')

Low no of months.

Select datediff(month, '2014-09-01', '2016-03-31')
it should give 31 instead of 18.

I don't understand where i'm wrongUnsure


I'm not sure why you think there should be "31". I can understand why someone would insist that the correct answer is "19" but not "31".

with Numbers (Number) as
(select row_number() over (order by object_id) -1
from sys.all_objects)
select Number+1,dateadd(month, number, '2014-09-01')
from Numbers
where number <= datediff(month, '2014-09-01', '2016-03-31')
;




Results:

                     
-------------------- -----------------------
1 2014-09-01 00:00:00.000
2 2014-10-01 00:00:00.000
3 2014-11-01 00:00:00.000
4 2014-12-01 00:00:00.000
5 2015-01-01 00:00:00.000
6 2015-02-01 00:00:00.000
7 2015-03-01 00:00:00.000
8 2015-04-01 00:00:00.000
9 2015-05-01 00:00:00.000
10 2015-06-01 00:00:00.000
11 2015-07-01 00:00:00.000
12 2015-08-01 00:00:00.000
13 2015-09-01 00:00:00.000
14 2015-10-01 00:00:00.000
15 2015-11-01 00:00:00.000
16 2015-12-01 00:00:00.000
17 2016-01-01 00:00:00.000
18 2016-02-01 00:00:00.000
19 2016-03-01 00:00:00.000

(19 row(s) affected)



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
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