Date Problem

pts.
Tags:
SQL Server
I am trying to create a query in SQL Server that will generate a list of dates starting from Jan 1 2007 and ending Dec 31. I have been able to get them in columns, but I want to get them in rows. Please note that I need the query to dynamically generate the data I want to avoid creating tables. Is this possible using just a query? Please help.

Answer Wiki

Thanks. We'll let you know when a new response is added.

Hello,

What about using a user defined function? I mean something like this:

CREATE FUNCTION CreateDateList ( @iniDate DATETIME, @endDate DATETIME )
RETURNS @DateList TABLE
(
Dates DATETIME
)
AS
BEGIN

DECLARE @workDate DATETIME

SET @workDate = @iniDate – 1

WHILE @workDate < @endDate
BEGIN
SET @workDate = @workDate + 1
INSERT @DateList VALUES (@workDate)
END
RETURN
END

Then you could use

SELECT *
FROM CreateDateList
(’01/01/07′, ’31/12/07′) –Likely ’12/31/07′ for you

to get all the rows you need.

Judith

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following