Solved

Calculating military time with a start and end time

Posted on 2013-05-21
18
479 Views
Last Modified: 2013-07-03
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
Comment
Question by:Sherry
  • 11
  • 5
18 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 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

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

Author Comment

by:Sherry
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Sherry
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 45

Expert Comment

by:Kdo
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

by:Sherry
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

by:Sherry
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 45

Assisted Solution

by:Kdo
Kdo earned 500 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

by:Sherry
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 45

Expert Comment

by:Kdo
ID: 39211886
That'd help...
0
 

Author Comment

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

Author Comment

by:Sherry
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

by:Sherry
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

by:Sherry
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 45

Expert Comment

by:Kdo
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

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

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

770 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