Sherry
asked on
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
Example: Parameters - Start time 0700, End Time 1600
Results:
0700
0800
0900
1000
1100
1200
1300
1400
1500
1600
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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'),('0 800')
,('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
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')
,('0900'),('1000'),('1100'
,('1700'),('1800'),('1900'
SELECT MHours
FROM #MilHours
WHERE MHours BETWEEN @StartTime AND @EndTime
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
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
ASKER
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.
ASKER
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)
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)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
That'd help...
ASKER
Here you go
USE-Callout.docx
USE-Callout.docx
ASKER
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.
ASKER
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.
ASKER
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.
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.
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
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
ASKER
All of the help I received did help me to get what I needed. Thank you
ASKER