December 10, 2008 at 1:47 am
Dear all,
i need to display dates as a O/P which falls between two date inputs .
Eg:
input 1 : 01-12-2008
Input 2 : getdate()
O/P should be print like : 01-12-2008 .... till getdate .
can anyone help me out to sort out this issue .. but this o/p should be generate by select statement . without using functions or procedure .
pls help
Thanks in advance!
regards,
Sabari.C
December 10, 2008 at 1:51 am
SELECT Date
FROM Calendar
WHERE Date BETWEEN @StartDate and @EndDate
_____________
Code for TallyGenerator
December 10, 2008 at 1:55 am
Why do you want to get the dates only during run time? With SQL Server 2005 you can use Common Table Expression, but since you are working with SQL Server 2000, this is not possible. Unfortunately I’m not aware of any way to do it without using functions (or any other code) or dates table. The question is why not use any of them (function or dates table)?
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
December 10, 2008 at 1:56 am
thnaks for your reply . but i should not use table fields . without using how can we derive the o/p .
December 10, 2008 at 2:29 am
sabarichandru (12/10/2008)
but i should not use table fields .
Why?
Is it kind of assessment?
_____________
Code for TallyGenerator
December 10, 2008 at 2:41 am
Let me see if I understand you. You need to show a list of dates. Not only that the dates are important, it is also important that it will not be from a table, and it will not be calculated from a function. While I can understand that it is important that you’ll have the correct dates, I really can’t understand why it can’t be done using a table or a function. The user has to get the dates. The user shouldn’t know nor care how he got the dates. Can you try and explain your situation?
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
December 10, 2008 at 3:50 am
sabarichandru (12/10/2008)
Dear all,i need to display dates as a O/P which falls between two date inputs .
Eg:
input 1 : 01-12-2008
Input 2 : getdate()
O/P should be print like : 01-12-2008 .... till getdate .
can anyone help me out to sort out this issue .. but this o/p should be generate by select statement . without using functions or procedure .
pls help
Thanks in advance!
regards,
Sabari.C
Do you want your dates all in one column or in one row?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 10, 2008 at 4:29 am
anyway with the help of table only i will derive date value .. but for test purpose they ask me not to use tables . directly they ask me specify the date values .
December 10, 2008 at 4:30 am
Hi chris,
i need a o/p in column .
December 10, 2008 at 4:32 am
Hi sergy,
its not a assiginment .. part of my project . but they ask me not to use table fields directly u should specify the values to derive the date o/p .
December 10, 2008 at 4:42 am
sabarichandru (12/10/2008)
Hi chris,i need a o/p in column .
This will do it:
DECLARE @InputDate DATETIME, @DayCount INT
SET @InputDate = '01-12-2008'
SET @DayCount = 1 + DATEDIFF(dd, @InputDate, GETDATE())
SELECT DATEADD(dd, 1-N.number, GETDATE()) AS MyDates
FROM Numbers N
WHERE N.number <= @DayCount
If you don't already have a tally, or numbers, table then you will need one for this code to work. Read about the tally table here:
http://www.sqlservercentral.com/articles/TSQL/62867/
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
December 10, 2008 at 11:43 am
sabarichandru (12/10/2008)
Hi sergy,its not a assiginment .. part of my project . but they ask me not to use table fields directly u should specify the values to derive the date o/p .
If it's your project why anybody tell you how to do things?
You should choose the most optimal solution, not the one someone prefers.
And reading from table is the one to choose.
Why? Because static table takes some amount of memory once when it's populated and then it's shared amongst all users need to read it.
Derived table is built in memory on fly, 1 table per user session.
In busy system amount of memory taken for all active sessions can kill the system performance.
Of course, you're not gonna discoverer it until system goes live and gets reasonable amount of users.
If you're a smart contractor you're gonna be long gone before this time.
And desperate owner of the project will face bankruptcy unless they can find someone like me who can apply appropriate database solutions.
_____________
Code for TallyGenerator
December 10, 2008 at 9:42 pm
Sure, but as others have already stated, a table source for this is the best way to go. The following code will solve the problem without a table, though. Courtesy of Itzek...
DECLARE @StartDate DATETIME,
@EndDate DATETIME
SELECT @StartDate = '01-12-2008',
@EndDate = DATEDIFF(dd,0,GETDATE())
;WITH
L0 AS (SELECT 1 AS C UNION ALL SELECT 1), --2 rows
L1 AS (SELECT 1 AS C FROM L0 AS A, L0 AS B),--4 rows
L2 AS (SELECT 1 AS C FROM L1 AS A, L1 AS B),--16 rows
L3 AS (SELECT 1 AS C FROM L2 AS A, L2 AS B),--256 rows
L4 AS (SELECT 1 AS C FROM L3 AS A, L3 AS B),--65536 rows
L5 AS (SELECT 1 AS C FROM L4 AS A, L4 AS B),--4294967296 rows
Nums AS (SELECT ROW_NUMBER() OVER (ORDER BY C) AS N FROM L5)
SELECT N-1+@StartDate FROM Nums WHERE N <= DATEDIFF(dd,@StartDate,@EndDate)+1
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2008 at 10:27 pm
Jeff, I know, you've got SQL2k5 to play with, but this is SQL2k forum, remember?
😉
_____________
Code for TallyGenerator
December 11, 2008 at 3:47 am
hII jEFF
Thanks a lot
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply