Solved

Calculating military time with a start and end time

Posted on 2013-05-21
18
474 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
 

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

18 Experts available now in Live!

Get 1:1 Help Now