English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a field in my database with the following data in it:
"12/1/2004 10:02:14 AM"
They are the time and date the sales transaction took place. I would like to SUM all of the sales that took place during the 10 AM hour, 11 AM hour, 12 pm hour, and so on. How do I select out only the hour characters and the am/pm characters?

2007-07-17 07:28:44 · 3 answers · asked by parkerjr2 3 in Computers & Internet Programming & Design

Using SQL code

2007-07-17 08:18:36 · update #1

3 answers

You can use the DATEPART() function if you're only concerned with the hour.

Note that this query assumes you don't care about the date, just the hour of the day. It will sum all of the sales that took place in the 10 o'clock hour, regardless of the date. You weren't really specific as to whether or not the date mattered. It would be easy to modify to take this into account.

SELECT
COUNT(SalesID) AS NumberOfSales,
DATEPART(hh, TransactionDate) AS HourOfDay

FROM

Sales

GROUP BY DATEPART(hh, TransactionDate)

ORDER BY DATEPART(hh, TransactionDate)


Note that this will use military time to handle AM/PM.

2007-07-17 10:21:45 · answer #1 · answered by kryzchek79 5 · 0 0

without myself looking into it deeply a CASE statement in the SQL query might do the trick but I am not sure

Sum(case ....end case)

2007-07-17 15:20:44 · answer #2 · answered by SEAN W 2 · 0 0

Tedious, but:



2007-07-17 14:57:23 · answer #3 · answered by fjpoblam 7 · 0 0

fedest.com, questions and answers