sql - Query to return +- 30 days from a specific date -
i'm trying figure out how write query return table of 61 record list date each record current date.
this useful function use, taken here:
explode dates between dates, check , adjust parameter
just send date-30 , date+30
create function [dbo].[explodedates] (@startdate datetime, @enddate datetime) returns table return ( n0 (select 1 n union select 1) ,n1 (select 1 n n0 t1, n0 t2) ,n2 (select 1 n n1 t1, n1 t2) ,n3 (select 1 n n2 t1, n2 t2) ,n4 (select 1 n n3 t1, n3 t2) ,n5 (select 1 n n4 t1, n4 t2) ,n6 (select 1 n n5 t1, n5 t2) ,nums (select row_number() on (order (select 1)) num n6) select dateadd(day, num-1, @startdate) thedate nums num <= datediff(day, @startdate, @enddate) + 1 ); go
if don't want function, can use query, declaring
@startdate = @mydate - 30
,
@enddate = @mydate + 30
Comments
Post a Comment