Solved

SQL Datepart and Convert DATETIME functions

Posted on 2009-04-08
9
746 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

746 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

12 Experts available now in Live!

Get 1:1 Help Now