Saturday, February 25, 2012

Calendar Table

I am trying to get all dates starting from June 20 , 2004 to yesterday but
the table includes today's that (which i dont want).
DECLARE @.Tmp TABLE (DOM smalldatetime)
DECLARE @.day smalldatetime
DECLARE @.DOM smalldatetime
SET @.day = 'June 20, 2004'
WHILE @.day < GetDate()
BEGIN
INSERT INTO @.Tmp
SELECT @.day as DOM
SET @.Day = @.Day + 1
END
@.day is not < GETDATE() until it hits the next day (because of the time
component). You need to remove the time component from GETDATE() somehow.
I have done so in the example below:
DECLARE @.Tmp TABLE (DOM smalldatetime)
DECLARE @.day smalldatetime
DECLARE @.DOM smalldatetime
SET @.day = 'June 20, 2004'
WHILE @.day < CONVERT(datetime,CONVERT(char(8),GetDate(),112))
BEGIN
INSERT INTO @.Tmp
SELECT @.day as DOM
SET @.Day = @.Day + 1
END
Keith
"Asim" <Asim@.discussions.microsoft.com> wrote in message
news:090F9AA5-BF5D-44C4-9405-96D6D7276493@.microsoft.com...
>I am trying to get all dates starting from June 20 , 2004 to yesterday but
> the table includes today's that (which i dont want).
> DECLARE @.Tmp TABLE (DOM smalldatetime)
> DECLARE @.day smalldatetime
> DECLARE @.DOM smalldatetime
> SET @.day = 'June 20, 2004'
> WHILE @.day < GetDate()
> BEGIN
> INSERT INTO @.Tmp
> SELECT @.day as DOM
> SET @.Day = @.Day + 1
> END
|||Thankyou very much..
"Keith Kratochvil" wrote:

> @.day is not < GETDATE() until it hits the next day (because of the time
> component). You need to remove the time component from GETDATE() somehow.
> I have done so in the example below:
> DECLARE @.Tmp TABLE (DOM smalldatetime)
> DECLARE @.day smalldatetime
> DECLARE @.DOM smalldatetime
> SET @.day = 'June 20, 2004'
> WHILE @.day < CONVERT(datetime,CONVERT(char(8),GetDate(),112))
> BEGIN
> INSERT INTO @.Tmp
> SELECT @.day as DOM
> SET @.Day = @.Day + 1
> END
>
> --
> Keith
>
> "Asim" <Asim@.discussions.microsoft.com> wrote in message
> news:090F9AA5-BF5D-44C4-9405-96D6D7276493@.microsoft.com...
>
>

No comments:

Post a Comment