Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-01-25 : 11:25:06
|
Assume the time now is 8:15:00I would like to show the records from tblMain which do not fall within the fromTime and toTime in tblTimes.Example:tblTimesID fromTime toTime123 1900-01-01 07:40:00 1900-01-01 09:00:00123 1900-01-01 09:50:00 1900-01-01 10:30:00...tblMainID fieldName1 fieldName2123...123...select * from tblMainif (ID in tblMain is present in tblTimes and the present time falls between fromTime and toTime then do not show the ID i.e. 123 )so the result should show:ID fieldName1 fieldName2123......BUT if the time now is 07:35:00the result of the select query should be the following as the present time does not fall within the fromTime and toTime:ID fieldName1 fieldName2123...123...I have the following query but it does not seem correct.declare @CurrentTime varchar(20)set @CurrentTime = convert(varchar(10),getdate(),108) select......from @tblmain as rleft join tblTimes as s on r.ID = s.IDand convert(datetime, @CurrentTime) < convert(datetime, convert(varchar(10),s.fromTime,108))and convert(datetime, @CurrentTime) > convert(datetime, convert(varchar(10),s.toTime,108))Any thoughts please?Thanks |
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-26 : 07:42:49
|
Check this and let me know if it works? declare @CurrentTime Timeset @CurrentTime = cast(convert(varchar(10),getdate(),108) as time)Select tblMain.* From tblMain Inner Join tblTimes on tblTimes.ID=tblMain.ID And @CurrentTime < Cast (convert(varchar(5),tblTimes.FromTime,108) as time) And @CurrentTime > Cast (convert(varchar(5),tblTimes.ToTime,108) as Time) |
 |
|
arkiboys
Master Smack Fu Yak Hacker
1433 Posts |
Posted - 2011-01-26 : 07:56:35
|
As I am doing alot of processing, I have decided to do this in stages...And I am using between instead of > or <Thank you |
 |
|
|
|
|
|
|