Solved

converting decimal value to HH:MM

Posted on 2011-09-06
16
347 Views
Last Modified: 2012-05-12
Hi experts

Having difficulty converting a decimal value to a time value and I was wondering if someone could help me

I runa stored procedure which populates a table containing all the information about each student and what classes they attend.  I then search this table and work out how many hours each student is in college per day, total breaks per day, total time absent per day and so on

shown below is a snippet of code to show how I get total breaks (get the difference between start time and end time of day = 8 hours then subtract the sum of all the classes that day)
For this example shown in spreadsheet because two classes are timetabled for the same time (which should not happen) the total hours per day is 8 hours and 15 mins giving the result total breaks being -0.250000.  I want to keep this as a negative number because the user can easily see that the timetable is wrong but what I would like to do is display the total breaks for this example as -0.15 instead of -0.250000

Thanks in advance
SELECT  student_id, Date,week_no, 
		MIN(CONVERT(datetime, start_time)) AS 'Start Time', 
		MAX(CONVERT(datetime, End_Time)) AS 'End Time',
		ROUND(cast((datediff(mi, MIN(start_time), MAX(end_time))  / 60.0) as FLOAT),2) AS 'Total Hours',
		(DATEDIFF(minute, MIN(start_time), MAX(end_time)) - SUM(DATEDIFF(minute, start_time, end_time))) / 60.0 AS 'Total Breaks'

from allstudents

Open in new window

sample-student.xls
0
Comment
Question by:lisa_mc
  • 9
  • 6
16 Comments
 
LVL 3

Author Comment

by:lisa_mc
ID: 36488087
I use this line below to convert decimal to time

convert(varchar, convert(int,floor(0.25)))+':'+
      replicate('0',(2 - len(convert (varchar, convert(int,(0.25 - floor(0.25)) * 60.0)))))
      +convert (varchar, convert(int,(0.25 - floor(0.25)) * 60.0)) as 'Total Breaks',

This gives the value 0:15 so there is 15 mins break which is correct

but when I enter -0:25 I get the value -1:45

is there anyway around this so that the result will show -0:15
0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 36488283
Your first problem here probably is that floor(0.25) is 0, but floor(-0.25) is -1, you should obtain the absolute value of your data before applying floor function.

I don't remember exactly now, but I think sql server has an ABS function to that?
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36488500
Hi Bardobrave

I was looking at ABS function and it converts all the negatives into postivie values which isn't correct

Any other suggestions
0
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36488601
Hi lisa_mc:,

What about this:

convert(varchar, convert(int,(-1.25-(-1.25%1))))+':'+ convert(varchar,  convert(int,60*ABS(-1.25%1))) as 'Total Breaks'

HTH,

Mark
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36488683
Hi OnALearningCurve

Thanks for the reply but the example Im using is -0.25 and not -1.25 and when I enter -0.25 I get the result 0.15

also the value -0.25 can change this is just an example I came across
0
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36488770
OK,

not the fix you are after but this:

(-1.25-(-1.25%1))*60 + 60*ABS(-1.25%1)

should give you the total break as a total value in minutes.  I now just need to figure out how to convert this in to the format of hh:mm

leave it with me
0
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36488863
OK,

It's a bit of a cludge but how about this:

(CASE WHEN -0.25 < 0 THEN '-' END) + convert(varchar, convert(int,ABS(-0.25-(-0.25%1))))+':'+ convert(varchar,  convert(int,60*ABS(-0.25%1))) as 'Total Breaks'

Hope this helps,

Mark
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36488872
ok mark

that would be great thanks very much
0
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.

 
LVL 3

Author Comment

by:lisa_mc
ID: 36488876
sorry just noticed your post will try that thanks
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36488986
Hi again mark

2 problems firstly when value = -2:00 the code above shows -2:0 is there a way to show the extra 0 at the end

secondly - the code below is your code but it only caters for when total breaks is less than 0

(CASE WHEN [total breaks] < 0 THEN '-' END) + convert(varchar, convert(int,ABS([total breaks]-([total breaks]%1))))+
      ':'+ convert(varchar,  convert(int,60*ABS([total breaks]%1))) as 'Total Breaks',

I need a way of adding this onto your code when total breaks > 0

convert(varchar, convert(int,floor([Total Breaks])))+':'+
      replicate('0',(2 - len(convert (varchar, convert(int,([Total Breaks] - floor([Total Breaks])) * 60.0)))))
      +convert (varchar, convert(int,([Total Breaks] - floor([Total Breaks])) * 60.0)) as 'Total Breaks',

as the above converts the positive values to time values

Thanks
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36489053
sorry got the two combined

too many brackets was gettin confused :-)

So just need to know is there a way of adding the extra 0 at the end?

Thanks
(CASE WHEN [total breaks] < 0 THEN '-' + convert(varchar, convert(int,ABS([total breaks]-([total breaks]%1))))+
	':'+ convert(varchar,  convert(int,60*ABS([total breaks]%1))) 
	 
	else
	convert(varchar, convert(int,floor([total breaks])))+':'+
	replicate('0',(2 - len(convert (varchar, convert(int,([total breaks] - floor([total breaks])) * 60.0)))))
	+convert (varchar, convert(int,([total breaks] - floor([total breaks])) * 60.0))

	end) as 'Total Breaks',

Open in new window

0
 
LVL 10

Accepted Solution

by:
OnALearningCurve earned 500 total points
ID: 36489111
Try this:

(CASE WHEN -2 < 0 THEN '-' ELSE '' END) + convert(varchar, convert(int,ABS(-2-(-2%1))))+':'+ (CASE WHEN LEN(convert(varchar,  convert(int,60*ABS(-2%1)))) = 1 THEN '0'+convert(varchar,  convert(int,60*ABS(-2%1))) ELSE convert(varchar,  convert(int,60*ABS(-2%1))) END) as 'Total Breaks'

I think I might have nailed it,

Cheers,

Mark.
0
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36489294
As an alternative,

you could replace the second CASE statement with a select RIGHT:

(CASE WHEN -2 < 0 THEN '-' ELSE '' END) + convert(varchar, convert(int,ABS(-2-(-2%1))))+':'+ RIGHT(('0'+convert(varchar,  convert(int,60*ABS(-2%1)))),2) as 'Total Breaks'

you should get the same results but this version s a little shorter and I would guess a little less overhead.

Hope this helps,

Mark
0
 
LVL 3

Author Closing Comment

by:lisa_mc
ID: 36489307
perfect solution thank you so much for sticking with the question

appreciate it thanks
0
 
LVL 3

Author Comment

by:lisa_mc
ID: 36489311
again our posts crossed

thank you for the alternative
0
 
LVL 10

Expert Comment

by:OnALearningCurve
ID: 36489354
No Problem,

Glad I could help and thanks for the points :)
0

Featured Post

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.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

746 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

10 Experts available now in Live!

Get 1:1 Help Now