Solved

# converting decimal value to HH:MM

Posted on 2011-09-06
354 Views
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

``````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
``````
sample-student.xls
0
Question by:lisa_mc
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 9
• 6

LVL 3

Author Comment

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

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

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

ID: 36488601
Hi lisa_mc:,

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

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

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

ID: 36488863
OK,

(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

ID: 36488872
ok mark

that would be great thanks very much
0

LVL 3

Author Comment

ID: 36488876
sorry just noticed your post will try that thanks
0

LVL 3

Author Comment

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

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',
``````
0

LVL 10

Accepted Solution

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

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

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

appreciate it thanks
0

LVL 3

Author Comment

ID: 36489311
again our posts crossed

thank you for the alternative
0

LVL 10

Expert Comment

ID: 36489354
No Problem,

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Read about achieving the basic levels of HRIS security in the workplace.
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â€¦
###### Suggested Courses
Course of the Month2 days, 8 hours left to enroll