Sql 在一系列日期之间获取日期
我需要使用SQL Server 2005获取日期范围中的所有日期,如果您在表中有日期,并且只想在两个日期之间选择可以使用的日期Sql 在一系列日期之间获取日期,sql,sql-server,tsql,datetime,date-range,Sql,Sql Server,Tsql,Datetime,Date Range,我需要使用SQL Server 2005获取日期范围中的所有日期,如果您在表中有日期,并且只想在两个日期之间选择可以使用的日期 select * from yourTable where yourDate between date1 and date2 如果您想从无到有地生成日期,可以使用循环或使用日期填充临时表,然后从中进行选择。要生成日期范围,可以编写表值函数。这是一个为数据仓库创建日期维度的函数-您可以通过删除特殊项来相当容易地调整它 编辑:此处没有日期维度层次结构 if object_
select * from yourTable where yourDate between date1 and date2
如果您想从无到有地生成日期,可以使用循环或使用日期填充临时表,然后从中进行选择。要生成日期范围,可以编写表值函数。这是一个为数据仓库创建日期维度的函数-您可以通过删除特殊项来相当容易地调整它 编辑:此处没有日期维度层次结构
if object_id ('ods.uf_DateHierarchy') is not null
drop function ods.uf_DateHierarchy
go
create function ods.uf_DateHierarchy (
@DateFrom datetime
,@DateTo datetime
) returns @DateHierarchy table (
DateKey datetime
) as begin
declare @today datetime
set @today = @Datefrom
while @today <= @DateTo begin
insert @DateHierarchy (DateKey) values (@today)
set @today = dateadd (dd, 1, @today)
end
return
end
go
给你:
DECLARE @DateFrom smalldatetime, @DateTo smalldatetime;
SET @DateFrom='20000101';
SET @DateTo='20081231';
-------------------------------
WITH T(date)
AS
(
SELECT @DateFrom
UNION ALL
SELECT DateAdd(day,1,T.date) FROM T WHERE T.date < @DateTo
)
SELECT date FROM T OPTION (MAXRECURSION 32767);
如果您希望在两个日期之间获得数据库中的所有日期,即客户在2008年第3季度下订单的日期,您可以这样写:
select distinct(orderPlacedDate)
from orders
where orderPlacedDate between '2008-07-01' and 2008-09-30'
order by orderPlacedDate
以下是Oracle版本的日期生成:
SELECT TO_DATE ('01-OCT-2008') + ROWNUM - 1 g_date
FROM all_objects
WHERE ROWNUM <= 15
它可以是任何具有足够行以覆盖所需范围的表,而不是所有对象。稍微复杂一点,但可能更灵活的是使用包含一组连续数字的表。这允许使用不同间隔的多个日期范围
/* holds a sequential set of number ie 0 to max */
/* where max is the total number of rows expected */
declare @Numbers table ( Number int )
declare @max int
declare @cnt int
set @cnt = 0
/* this value could be limited if you knew the total rows expected */
set @max = 999
/* we are building the NUMBERS table on the fly */
/* but this could be a proper table in the database */
/* created at the point of first deployment */
while (@cnt <= @max)
begin
insert into @Numbers select @cnt
set @cnt = @cnt + 1
end
/* EXAMPLE of creating dates with different intervals */
declare @DateRanges table (
StartDateTime datetime, EndDateTime datetime, Interval int )
/* example set of date ranges */
insert into @DateRanges
select '01 Jan 2009', '10 Jan 2009', 1 /* 1 day interval */
union select '01 Feb 2009', '10 Feb 2009', 2 /* 2 day interval */
/* heres the important bit generate the dates */
select
StartDateTime
from
(
select
d.StartDateTime as RangeStart,
d.EndDateTime as RangeEnd,
dateadd(DAY, d.Interval * n.Number, d.StartDateTime) as StartDateTime
from
@DateRanges d, @Numbers n
) as dates
where
StartDateTime between RangeStart and RangeEnd
order by StartDateTime
实际上,我使用了一种变体,将日期划分为不同的时间段,但通常为5分钟长。My@numbers表最多包含288个插槽,因为这是您在24小时内可以拥有的5个最小插槽的总数
/* EXAMPLE of creating times with different intervals */
delete from @DateRanges
/* example set of date ranges */
insert into @DateRanges
select '01 Jan 2009 09:00:00', '01 Jan 2009 12:00:00', 30 /* 30 minutes interval */
union select '02 Feb 2009 09:00:00', '02 Feb 2009 10:00:00', 5 /* 5 minutes interval */
/* heres the import bit generate the times */
select
StartDateTime,
EndDateTime
from
(
select
d.StartDateTime as RangeStart,
d.EndDateTime as RangeEnd,
dateadd(MINUTE, d.Interval * n.Number, d.StartDateTime) as StartDateTime,
dateadd(MINUTE, d.Interval * (n.Number + 1) , StartDateTime) as EndDateTime
from
@DateRanges d, @Numbers n
) as dates
where
StartDateTime >= RangeStart and EndDateTime <= RangeEnd
order by StartDateTime
您想生成日期,还是只搜索现有字段?Harish,请将下面的一个答复标记为答案。他们花时间来帮助你,我很喜欢这个解决方案。对时间线使用类似的方法,这样即使数据库中没有匹配该时间段的记录,图表也会保持一致的间隔。对。这适用于生成任何序列:只需将DateAddday,1,T.date替换为其他一些This_item=Fprevious_item公式非常酷的CTE解决方案。我以前没见过这么做。这个石头。我需要一些能让我在两次约会之间度过所有周一的东西。所以我使用了一个额外的过滤器,使用datepart和bam。。。正是我需要的+1来自我。这只适用于较小的日期范围。少于2048天。
DECLARE @Date1 DATE='2016-12-21', @Date2 DATE='2016-12-25'
SELECT DATEADD(DAY,number,@Date1) [Date] FROM master..spt_values WHERE type = 'P' AND DATEADD(DAY,number,@Date1) <= @Date2