Solved

sql query date time

Posted on 2012-04-02
6
254 Views
Last Modified: 2012-04-02
Hi!

i have 2 datetime fields  OnDateTime, OfDateTime

OnDateTime  2012-04-02 18:00:00
OffDateTime 2012-12-01  23:00:00


the query needs to return data between the dates but only between 18:00:00 -23:00:00 every day between the 2 dates. i'e it should not return anything if query is run 2012-04-03 16:00:00 but it is to return data if query is run 2012-04-03 18:01:00.  for now i'm dealing with this programatically but i would like to use a query for it.
0
Comment
Question by:jamppi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37795893
Try this

select * from <TableName>
where convert(nvarchar,OnDateTime,108) between '18:00:00' and '23:00:00'
and convert(nvarchar,OffDateTime,108) between '18:00:00' and '23:00:00'
and convert(nvarchar,OnDateTime,101) between '04/02/2012' and '12/01/2012' 
and convert(nvarchar,OffDateTime,101) between '04/02/2012' and '12/01/2012' 

Open in new window

0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37795901
Or

select * from <TableName>
where (convert(nvarchar,OnDateTime,108) >= '18:00:00' and convert(nvarchar,OffDateTime,108) <= '23:00:00')
and (convert(nvarchar,OnDateTime,101) >= '04/02/2012' and  convert(nvarchar,OffDateTime,101) <= '12/01/2012')

Open in new window

0
 
LVL 18

Expert Comment

by:lludden
ID: 37796005
Do OnDateTime and OffDateTime represent periods that you need to see if they intersect with the periods you are looking at?

If OnDateTime = '2012-04-02 17:00:00' AND OffDateTime = '2012-04-03 02:00:00' would it show up?
Are both fields on the same row in the table?

For instance, we use similar fields when doing resource tracking, and need to see whomever had a resource during each shift.  There are four options:
1. Have it assigned prior to shift and keep until after shift
2. Have it assigned prior to shift and return it during shift.
3. Have it assigned during shift and return after shift
4. Have it assigned during shift and returned during shift.

Which conditions are you looking for?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:jamppi
ID: 37796042
OndateTime                        OffDateTime                    Data
2012-04-02 18:00:00          2012-10-02 21:00:00        test 1
2012-04-02 19:00:00          2012-10-02 21:00:00        test 2
2012-04-02 20:00:00          2012-10-02 21:00:00        test 3
2012-04-02 21:00:00          2012-10-02 22:00:00        test 4
2012-04-03 18:00:00          2012-10-02 23:00:00        test 5

If I'm querying the database at 2012-04-02 12:00:00 (getdate())  it should not return anything as you can see.

But if I'm querying at  2012-04-03 19:01:00   i should get  test1 and test 2

I hope this clarifies the question
0
 
LVL 18

Accepted Solution

by:
lludden earned 500 total points
ID: 37796168
I am still not 100% sure of what you want, but here is a try.  If this isn't it, post the code you are using and I can convert it to a query.

DECLARE @T1 TABLE (OnDatTime DATETIME, OffDateTime DATETIME, Data VARCHAR(10))
INSERT INTO @T1 
SELECT '2012-04-02 18:00:00', '2012-10-02 21:00:00','test 1' UNION
SELECT '2012-04-02 19:00:00', '2012-10-02 21:00:00','test 2' UNION
SELECT '2012-04-02 20:00:00', '2012-10-02 21:00:00','test 3' UNION
SELECT '2012-04-02 21:00:00', '2012-10-02 22:00:00','test 4' UNION
SELECT '2012-04-03 18:00:00', '2012-10-02 23:00:00','test 5'

DECLARE @QueryAsOf datetime = '2012-04-03 19:01:00'

SELECT Data FROM @T1 T1
WHERE cast(convert(VARCHAR(5),@QueryAsOf,108) AS DATETIME) BETWEEN cast(convert(VARCHAR(5),OnDatTime,108) AS DATETIME) AND cast(convert(VARCHAR(5),OffDateTime,108) AS DATETIME)
AND DATEADD(DAY, DATEDIFF(DAY, 0, T1.OnDatTime), 0) < DATEADD(DAY, DATEDIFF(DAY, 0, @QueryAsOf), 0)

Open in new window

0
 

Author Closing Comment

by:jamppi
ID: 37796574
Perfect!
Thank you for the quick solution.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question