Hi all,
Right i have created a application thats connects to a SQL Server 2008 database, the table has three datetime fields, I have connected to the database via a data adapter. I have four radio buttons on form which allow user to select what tasks to display from database. One of the radio buttons is to show tasks that are required by five days or less. The field i need to check against is called Required_By, So how do i go about writing the sql statement for this query, I need a way of comparing two dates, i have heard of a timespan function but havent used this before, Thanks for any help before hand
Cheers,
Nathan
2 replies to this topic
#1
Posted 02 December 2010 - 08:16 PM
|
|
|
#2
Posted 02 December 2010 - 11:21 PM
Hi Nathan,
I don't know about timespan function, but for this problem I usually use DATEDIFF function. Using it your query is going to be something like:
Note that this query could not take advantage of any available indexes, making this query in table with large rows a sluggish runner since it will scan the entire table. Therefore I suggest to add another criteria to limit the rows it have to scan, for example maybe you want to limit the start date into some small range.
Beside using DATEDIFF, you could also use DATEADD. But I think it's easier to read by using DATEDIFF.
Cheers!
I don't know about timespan function, but for this problem I usually use DATEDIFF function. Using it your query is going to be something like:
SELECT * FROM Tabel T WHERE DATEDIFF(day, FieldDate1, FieldDate2) <= 5
Note that this query could not take advantage of any available indexes, making this query in table with large rows a sluggish runner since it will scan the entire table. Therefore I suggest to add another criteria to limit the rows it have to scan, for example maybe you want to limit the start date into some small range.
Beside using DATEDIFF, you could also use DATEADD. But I think it's easier to read by using DATEDIFF.
Cheers!
#3
Posted 03 December 2010 - 01:40 AM
thanks for feedback worked a treat :)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









