December 22, 2009 at 2:01 am
Hi all,
I created a table in which there r n fields and in that two of them are date field i.e., startdate and enddate.
Now i want to write a query,to fetch the records from table which comes under startdate and enddate.
so,i need a help how to write the query.
Thanks in advance.
December 22, 2009 at 3:04 am
What have you tried so far?
If nothing yet you might want to look into BETWEEN clause in BOL (BooksOnLine, the SQL Server help system).
December 22, 2009 at 3:14 am
khandaresarang (12/22/2009)
Hi all,I created a table in which there r n fields and in that two of them are date field i.e., startdate and enddate.
Now i want to write a query,to fetch the records from table which comes under startdate and enddate.
so,i need a help how to write the query.
Thanks in advance.
Can you provide the table creation script you used for this, and a series of INSERT statements which you may have used to populate the table? This will provide people with a starting point to help you.
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 22, 2009 at 3:35 am
If I understand you correctly we had a similar requirement where the input date range provided should overlap the StartDate and EndDate values in the records.
Say for example @StartDate and @EndDate are the input parameters and StartDate and EndDate are the field names then the query would be
SELECT columnlist FROM TableName WHERE StartDate <= @EndDate AND EndDate >= @StartDate
Thanks
Satish More
December 24, 2009 at 10:01 am
I agree with a slight modification if the EndDate is exclusive:
Logically you want all results from the table but only list results when the start date is before 'xx/xx/xxxx' and the end date is on/after 'xx/xx/xxxx'
SELECT columnlist FROM TableName WHERE StartDate < @EndDate AND EndDate >= @StartDate
You can do the following for a specific date:
select columnlist FROM TableName WHERE StartDate <= @TargetDate and @TargetDate < EndDate
January 5, 2010 at 4:28 am
U can use Between clause or datediff will be best i feel because between will not include the startdate and enddate.
But i am not understanding you question. Each record in the table has startdate and enddate right? do you want retrive all the records with common start and enddate?
Or if you have a want to retrive records in a perticular date range than u can use
select * from [tablename] where datediff(d,@startdate, start_date) >= 0 and datediff(d,@enddate, end_date) <= 0
here start_date and end_date are column names
January 5, 2010 at 5:02 am
select * from [tablename] where start_date >= @startdate and
end_date <=@enddate
January 5, 2010 at 6:11 am
geervani (1/5/2010)
U can use Between clause or datediff will be best i feel because between will not include the startdate and enddate.But i am not understanding you question. Each record in the table has startdate and enddate right? do you want retrive all the records with common start and enddate?
Or if you have a want to retrive records in a perticular date range than u can use
select * from [tablename] where datediff(d,@startdate, start_date) >= 0 and datediff(d,@enddate, end_date) <= 0
here start_date and end_date are column names
Using column names inside a function should be avoided as it will not utilize the index.
January 5, 2010 at 6:49 am
Agree I must with Chris. Help it would if OP would provide table DDL, sample data, and expected results.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply