[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Calculating military time with a start and end time

Posted on 2013-05-21
Medium Priority
493 Views
Is there a way to have the start and end time as parameters and the results would be the start time, 1 hr increments, end time.

Example:  Parameters - Start time 0700, End Time 1600

Results:
0700
0800
0900
1000
1100
1200
1300
1400
1500
1600
0
Question by:Sherry
[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
• 11
• 5

LVL 46

Accepted Solution

Kent Olsen earned 2000 total points
ID: 39185745
SQL Server doesn't have an "all values in range" function, so you have to simulate it.

Putting all of the values in a table and selecting the items in range will do it though.  Here's an example.

with MilHours (Hour, Text)
as
(
select 0000, '0000'
union all
select 0100, '0100'
union all
select 0200, '0200'
union all
select 0300, '0300'
)
select * from MilHours
where Hours between 0100 and 0200;

Kent
0

Author Comment

ID: 39185751
Great!, I'll give this a try.
0

Author Comment

ID: 39185834
Ok, here's what I did.

CREATE TABLE #MilHours (MHours CHAR(4))
INSERT INTO #MilHours (MHours)
(
SELECT 0700 '0700'
UNION ALL
SELECT 0800 '0800'
UNION ALL
SELECT 0900 '0900'
UNION ALL
SELECT 1000 '1000'
UNION ALL
SELECT 1100 '1100'
UNION ALL
SELECT 1200 '1200'
UNION ALL
SELECT 1300 '1300'
UNION ALL
SELECT 1400 '1400'
UNION ALL
SELECT 1500 '1500'
UNION ALL
SELECT 1600 '1600'
)
SELECT MHours from #MilHours
WHERE MHours between '0700' and '1400'
ORDER BY MHours

This works, except my results show 1000 - 1400 and I don't get  0700-0900.
0

Author Comment

ID: 39185897
Got it!  The following is what I decided on.  It works.

DECLARE @StartTime  CHAR(4)
DECLARE @EndTime      CHAR(4)

SET @StartTime = '0700'
SET @EndTime = '1400'

CREATE TABLE #MilHours (MHours CHAR(4))
INSERT INTO #MilHours (MHours)
VALUES ('0100'),('0200'),('0300'),('0400'),('0500'),('0600'),('0700'),('0800')
,('0900'),('1000'),('1100'),('1200'),('1300'),('1400'),('1500'),('1600')
,('1700'),('1800'),('1900'),('2000'),('2100'),('2200'),('2300'),('2400');

SELECT MHours
FROM #MilHours
WHERE MHours BETWEEN @StartTime AND @EndTime
0

LVL 46

Expert Comment

ID: 39185905
Hi Slusher,

The "trouble" was the conversion/manipulation of integers vs strings.  :)

You can always use the string form, as you have.  If you have a LOT of data you might store both the integer and string value in that table, then compare on the integer value.  Integers are faster than strings, though you'll probably not see a difference on only a few thousand rows.

Kent
0

Author Comment

ID: 39185938
Thank you.  I'm using this in a SSRS report.  I only need to pull some times to put in a schedule.  So, I'm going to try creating a stored procedure to get the dates, then put them in the columns on the report in two tables.  I have a main query for the report, I just need to add this so that it's one data source.
0

Author Comment

ID: 39211739
I actually have found a way to use a Dynamic Pivot table for this.  It works great when I run it in SQL but when I pull the query into my report (SSRS) I don't show any columns to put into my table.  Here's the part of the stored procedure for the final results and using the pivot.  Any help would be greatly appreciated.

DECLARE @QUERY CHAR(4000)
DECLARE @HOURS CHAR(2000)
SELECT @HOURS = STUFF((SELECT DISTINCT '],[' + MHours
FROM #MilHours
WHERE MHours BETWEEN @CalloutStartTime AND @CalloutEndTime
ORDER BY '],[' + MHours
FOR XML PATH('')
),1,2,'') + ']'

SET @QUERY =
'SELECT * FROM
(
SELECT
R.OffenderName
,R.DOCNumber
,R.OffenderUnit
,M.MHours
,0 AS X
FROM #MilHours M, #RESULTS R,#Reason RS
) T
PIVOT (SUM(X) FOR MHours
IN ('+@HOURS+'))AS  pvt'

EXECUTE (@QUERY)
0

LVL 46

Assisted Solution

Kent Olsen earned 2000 total points
ID: 39211799
Hi slusher,

It's hard to tell what's being executed, but I suspect that it has to do with data typing and conversion(s).

What is the value of @Hours?  It doesn't look like it should be formatted correctly.

Can you modify the second query to pivot on:

PIVOT (SUM(X) FOR MHours
BETWEEN @CalloutStartTime AND @CalloutEndTime) AS  pvt

Kent
0

Author Comment

ID: 39211828
No, it doesn't like the between and doesn't recognize the parameters.  If you look at the first part, @Hours is the MHours between the two parameters.  They way I have it, I can run it in SQL MS, and get what I want.  But when I try to use it in the rdl for the SSRS report, making it a dataset, I get no columns.  Do you need the full query?
0

LVL 46

Expert Comment

ID: 39211886
That'd help...
0

Author Comment

ID: 39211891
Here you go
USE-Callout.docx
0

Author Comment

ID: 39212071
I tried not doing the pivot in T-SQL and do it in the table/matrix.  It would work, but I keep getting the names list multiple times.
0

Author Comment

ID: 39281783
I was able to get this to work  by creating column or row groups in the report, after creating a temp table in my SQL to get the times.
0

Author Comment

ID: 39282137
I've requested that this question be closed as follows:

Accepted answer: 0 points for slslusher's comment #a39281783

for the following reason:

Information given by others help and I was able to figure it out from there through trial and error.
0

LVL 46

Expert Comment

ID: 39282138
Hi Slslusher,

Glad that you were able to solve this, but as you said, you didn't get there alone.  The Expert's Exchange protocols ask that you give credit to the answers that helped get you to the solution.

The question and answers will go into the Expert's Exchange library, called the PAQ (Previously Answered Questions).  Crediting the helpful posts gives acknowledgement to those people that assisted you and marks the useful responses for others that may read the thread to solve a similar issue.

Thanks,
Kent
0

Author Closing Comment

ID: 39297113
All of the help I received did help me to get what I needed.  Thank you
0

## Featured Post

Question has a verified solution.

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

Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article Iâ€™m going to show you another new feature of SSRS 2008 R2, this time in the visâ€¦
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and oâ€¦
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlinâ€¦
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produâ€¦
###### Suggested Courses
Course of the Month13 days, 8 hours left to enroll

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

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