• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

Calculating military time with a start and end time

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
Sherry
Asked:
Sherry
  • 11
  • 5
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
SherryAuthor Commented:
Great!, I'll give this a try.
0
 
SherryAuthor Commented:
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
SherryAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
SherryAuthor Commented:
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
 
SherryAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
SherryAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
That'd help...
0
 
SherryAuthor Commented:
Here you go
USE-Callout.docx
0
 
SherryAuthor Commented:
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
 
SherryAuthor Commented:
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
 
SherryAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
SherryAuthor Commented:
All of the help I received did help me to get what I needed.  Thank you
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now