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


Reg: Date Requirements


Reg: Date Requirements

Author
Message
vs.satheesh
vs.satheesh
SSC-Enthusiastic
SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)

Group: General Forum Members
Points: 187 Visits: 644
Hi

In my requirement
For example,

From date : 01-01-2012

To date : 30-12-2012


Intervel : 2 or 3 or 4 ....N



I want follwing result set based on the intervel.

suppose intervel 1 means

01-01-2012
03-01-2012
05-01-2012
.
.
.
.
30-12-2012


suppose intervel 2 means


01-01-2012
04-01-2012
07-01-2012
.
.
.
.
30-12-2012.



I want result based on intervel...
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39001 Visits: 38508
vs.satheesh (11/8/2012)
Hi

In my requirement
For example,

From date : 01-01-2012

To date : 30-12-2012


Intervel : 2 or 3 or 4 ....N



I want follwing result set based on the intervel.

suppose intervel 1 means

01-01-2012
03-01-2012
05-01-2012
.
.
.
.
30-12-2012


suppose intervel 2 means


01-01-2012
04-01-2012
07-01-2012
.
.
.
.
30-12-2012.



I want result based on intervel...





You may need to play with a little, but it seems to work based on your requirements. When I get more time, I look at it in more depth.



declare @StartDate date = '20120101',
@EndDate date = '20121230',
@Interval int = 2;

with e1(n) as (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)), -- 10 rows
e2(n) as (select 1 from e1 a cross join e1 b), -- 100 rows
e4(n) as (select 1 from e2 a cross join e2 b), -- 10,000 rows
eTally(n) as (select row_number() over (order by (select null)) from e4 a cross join e2 b) -- 1,000,000 rows
select top ((datediff(dd, @StartDate, @EndDate) + @Interval + 1)/(@Interval + 1))
dateadd(dd, (1 + @Interval) * (n - 1), @StartDate)
from
eTally





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)
Eugene Elutin
Eugene Elutin
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4960 Visits: 5478

DECLARE @Fromdate DATE = '01 Jan 2012'
,@Todate DATE = '30 Dec 2012'
,@Interval INT = 2

SELECT DateC , N
FROM ( SELECT DATEADD(DAY, N, @Fromdate ) AS DateC, N
FROM (SELECT (ROW_NUMBER() OVER (ORDER BY [object_id]) - 1) * (@Interval + 1)
FROM sys.columns) dt(N)
) R
WHERE DateC <= @Todate



Note that I used sys.columns as in-run-time tally table. You may want to create and use proper tally table.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
vs.satheesh
vs.satheesh
SSC-Enthusiastic
SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)SSC-Enthusiastic (187 reputation)

Group: General Forum Members
Points: 187 Visits: 644
Thank You
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