How to insert a time between the two dates
Using VB.Net and SQL 2005
Table1
Name FromDate ToDate
Sched1 20091201 20091231
Sched2 20090515 20090613
..开发者_Go百科.,
In table2 i want to add a time for a date between the FromDate and ToDate
Table2
Name Date StartTime EndTime
Sched1 20091201 080000 120000
...,
In my application am using "AddTime" Button. In the click event i want to write a code for adding the time for the dates.
When I click ADDTime Button. It should check the max(date) in table2. Then it should show the next date of the max(date) in table2.
For Example
Select max(date) from table where name = 'Sched1'.
It should show the next date means 20091202. because 20091201 Data is there.
There is no data in table2 for Sched2, So It should show the StartDate 20090515 from table1.
How to make a code or query for this condition?
Can any one give me a idea or samples for the above condition.
I believe following query will do the task at hand.
Select isnull(max(t2.date)+1,t1.FromDate)
from table1 t1 inner join table2 t2
on t1.[Name] = t2.[Name]
where t1.[name] = 'Sched1'
The query is joining both the tables on Name, and after that it is checking that, is there any date present in Table2 for 'Sched1', if so it will return the max date +1 day, and if not it will return the FromDate from Table1 for 'Sched1'.
I'll recommend you to read further on joins, you will not be able write but only the trivial queries without joins, further consider using integers for your keys.
精彩评论