converting decimal value to HH:MM

lisa_mc used Ask the Experts™
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

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


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
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?


Hi Bardobrave

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

Any other suggestions
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

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'




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

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

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,



ok mark

that would be great thanks very much


sorry just noticed your post will try that thanks


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



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?

(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))) 
	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

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,


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,



perfect solution thank you so much for sticking with the question

appreciate it thanks


again our posts crossed

thank you for the alternative
No Problem,

Glad I could help and thanks for the points :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial