RE: select date list
- From: JayAchTee <JayAchTee@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 23 Feb 2007 05:41:26 -0800
This Table-valued function should work as well:
USE [CaseStat_Tyco]
GO
/****** Object: UserDefinedFunction [dbo].[ufn_GenerateDateSequence]
Script Date: 02/23/2007 08:36:58 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ufn_GenerateDateSequence]
(
@StartAt DATETIME,
@EndAt DATETIME,
@Gap INT = 1
)
RETURNS
@localTAB TABLE
(
AutoKey DATETIME PRIMARY KEY
)
AS
BEGIN
DECLARE @count DATETIME
SET @count = @StartAt
WHILE (@count <= @EndAt)
BEGIN
INSERT INTO @localTAB VALUES(@count)
SET @count = DATEADD(day, @Gap, @count)
END
RETURN
END
using the T-SQL statement:
SELECT * FROM dbo.ufn_GenerateDateSequence('01/01/2007', '01/15/2007', 1)
the results are:
2007-01-01 00:00:00.000
2007-01-02 00:00:00.000
2007-01-03 00:00:00.000
2007-01-04 00:00:00.000
2007-01-05 00:00:00.000
2007-01-06 00:00:00.000
2007-01-07 00:00:00.000
2007-01-08 00:00:00.000
2007-01-09 00:00:00.000
2007-01-10 00:00:00.000
2007-01-11 00:00:00.000
2007-01-12 00:00:00.000
2007-01-13 00:00:00.000
2007-01-14 00:00:00.000
2007-01-15 00:00:00.000
--
Regards,
JayAchTee
"DavidBsaibes" wrote:
i am trying to write a query in sql server 2000 that returns the list of days.
between two dates.
it should return something similar to
for i = 1 to 100
select getdate() + i
next i
how can SQL server do such query.
thanks
- Prev by Date: Re: Returning Value to Web App via Stored Procedure
- Next by Date: Re: Date formatting - Really newbie question
- Previous by thread: Re: Returning Value to Web App via Stored Procedure
- Next by thread: Re: Date formatting - Really newbie question
- Index(es):