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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query help

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2014-08-07 : 04:08:15
I've got a table that tracks when a person is holding a specific token, e.g.

TokenId, PersonId, DateStart, DateEnd

I need to create a query to find which personId had a specific TokenId at the exact datetime specified.

How could I do that?

Thanks

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-07 : 07:56:32
[code]
select PersonId from <yourtable>
where TokenId = <a specific TokenId>
and DateStart = <exact datetime specified>
[/code]
Go to Top of Page

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2014-08-07 : 11:30:47
Hi,

The datetime which is specified should match the row where that datetime falls anytime between datestart and dateend.

Thanks
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-07 : 11:55:40
quote:
Originally posted by Mondeo

Hi,

The datetime which is specified should match the row where that datetime falls anytime between datestart and dateend.

Thanks



OK then:


select PersonId from <yourtable>
where TokenId = <a specific TokenId>
and <datetime> >= datestart and <datetime> < dateadd(second, 1, dateend)


Go to Top of Page
   

- Advertisement -