Solved

sql server - how to query specific time - T SQL

Posted on 2012-12-31
6
365 Views
Last Modified: 2012-12-31
I have a timestamp field.
I need to query all the records for the hour.. A record for 1,2,3 o'clock.
The data looks like

2012-11-01 01:00:00
2012-11-01 01:15:00
2012-11-01 01:45:00
2012-11-01 02:00:00
2012-11-01 02:15:00
2012-11-01 03:00:00

How do query for the full hour?
0
Comment
Question by:JElster
6 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38732995
This will give you the basic structure of the query:

select * from your_table
where column_name between convert(datetime, '2012-11-01 01:00:00', 120) and convert(datetime, '2012-11-01 02:00:00', 120)
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38733050
Just so you understand, a TimeStamp field is not the same as a Date/Time field.  According to Microsoft:

"It is used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The timestamp data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime data type."
0
 
LVL 1

Author Comment

by:JElster
ID: 38733066
It's not a timestamp field... sorry for the confusion
Just a DATETIME
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 69

Expert Comment

by:ScottPletcher
ID: 38733240
Most important are:
1) not to perform any function on the column itself
2) not to count the same row more than once


I strongly recommend the style below for ALL date/datetime checks:

--query for 1AM rows only
WHERE
    datetime_column >= '20121101 01:00:00' AND
    datetime_column <  '20121101 02:00:00'


Leaving out the dashes prevents any conversion errors ('YYYYMMDD' is always interpreted correctly) and/or misunderstandings about what the date value represents.

Using >= and < rather than between avoids counting rows on the exact hour twice.
0
 
LVL 1

Author Comment

by:JElster
ID: 38733500
But I have many dates!



What about this...
First

SUBSTRING( CONVERT(nvarchar(30), TIMESTAMP, 120),15,5) AS DT

then

select ....   WHERE DT = '00:00'
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 38733536
If you just want to group a lot of rows by their hours only, ignoring dates, do as below.

Yes, it uses a function on the column, but when you want all values you have to:


SELECT
    HOUR(TIMESTAMP) AS HR,
    ...
FROM dbo.tablename
--OPTIONAL!: for example, all records for current year only
--WHERE
    --TIMESTAMP >= '20120101' AND TIMESTAMP < '20130101'
GROUP BY
    HOUR(TIMESTAMP)
ORDER BY
    HOUR(TIMESTAMP)



If you want to see days, but only one row per hour per day, you can do this:

SELECT
    DATEADD(HOUR, DATEDIFF(HOUR, 0, TIMESTAMP), 0) AS DT,
    ...
FROM dbo.tablename
--OPTIONAL!: for example, all records for current year only
--WHERE
    --TIMESTAMP >= '20120101' AND TIMESTAMP < '20130101'
GROUP BY
    DATEADD(HOUR, DATEDIFF(HOUR, 0, TIMESTAMP), 0)
ORDER BY
    DATEADD(HOUR, DATEDIFF(HOUR, 0, TIMESTAMP), 0)
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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how the fundamental information of how to create a table.

747 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

10 Experts available now in Live!

Get 1:1 Help Now