Solved

SQL Datepart and Convert DATETIME functions

Posted on 2009-04-08
9
752 Views
Last Modified: 2012-05-06
I am trying to get a query to work.  I have a table that includes a column REQDATE that is a datetime type.  I need to return all records for a span of time REQDATE between '03/30/2009' and '04/06/2009'.  With the records returned I need to return all the records where the time is between 17:00:00 hours and 08:00:00 hours.  I am having trouble with this part.  The datetime is written like 08:00:00 AM, not in 24-hour time.
SELECT     *

FROM         TASKS

WHERE     (DATEPART(hh, REQDATE) BETWEEN '17:00:00' AND '08:00:00') AND (REQDATE BETWEEN CONVERT(DATETIME, '2009-03-30 00:00:00', 102) 

                      AND CONVERT(DATETIME, '2009-04-06 00:00:00', 102))

Open in new window

0
Comment
Question by:jeremymjackson
9 Comments
 
LVL 11

Expert Comment

by:N R
ID: 24098717

SELECT     *

FROM         TASKS

WHERE  Datepart > '17:00:00'

and Datepart < '08:00:00'

and ReqDate > CONVERT(DATETIME,'2009-03-30 00:00:00',102)

and ReqDate < CONVERT(DATETIME, '2009-04-06 00:00:00',102)

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24098730
wont something like this work?

WHERE
(datepart(hh,REQDATE)+':'+datepart(ss,REQDATE)+':'+datepart(ss,REQDATE)) BETWEEN '17:00:00' AND '08:00:00'
and
REQDATE BETWEEN CONVERT(DATETIME, '2009-03-30 00:00:00', 102)
                      AND CONVERT(DATETIME, '2009-04-06 00:00:00', 102))
0
 

Author Comment

by:jeremymjackson
ID: 24098812
SELECT     *  FROM         TASKS WHERE  Datepart > '17:00:00' and Datepart < '08:00:00' and ReqDate > CONVERT(DATETIME,'2009-03-30 00:00:00',102) and ReqDate < CONVERT(DATETIME, '2009-04-06 00:00:00',102)

Returns:  Msg 207, Level 16, State 1, Line 1
Invalid column name 'Datepart'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'Datepart'.
0
 

Author Comment

by:jeremymjackson
ID: 24098816
select * from tasks WHERE  (datepart(hh,REQDATE)+':'+datepart(ss,REQDATE)+':'+datepart(ss,REQDATE)) BETWEEN '17:00:00' AND '08:00:00' and REQDATE BETWEEN CONVERT(DATETIME, '2009-03-30 00:00:00', 102)                        AND CONVERT(DATETIME, '2009-04-06 00:00:00', 102))

returns:  Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Expert Comment

by:N R
ID: 24098836
Sorry, re read your question here you go:
SELECT     *

FROM         TASKS

WHERE ReqDate > CONVERT(DATETIME,'2009-03-30 17:00:00',102)

and ReqDate < CONVERT(DATETIME, '2009-04-06 08:00:00',102)

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 24099517
You want the hour > 17 or hour < 8.  It can't be both.

SELECT     *
FROM         TASKS
WHERE     REQDATE >= CONVERT(DATETIME, '2009-03-30 00:00:00', 102)
and reqdate <= CONVERT(DATETIME, '2009-04-06 00:00:00', 102))
and (DATEPART(hh, REQDATE) <8 or DATEPART(hh, REQDATE) > 17)
0
 

Author Comment

by:jeremymjackson
ID: 24099635
I want to return rows where the hour is between 17 and 8.
0
 

Author Closing Comment

by:jeremymjackson
ID: 31568096
BrandonGalderisi, Your query worked great.  Thanks.
0
 

Expert Comment

by:LLMTS
ID: 35336693
by any chance is this related to Track It?
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now