Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

converting decimal value to HH:MM

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
lisa_mc
Asked:
lisa_mc
  • 9
  • 6
1 Solution
 
lisa_mcAuthor Commented:
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
 
BardobraveCommented:
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
 
lisa_mcAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
OnALearningCurveCommented:
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
 
lisa_mcAuthor Commented:
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
 
OnALearningCurveCommented:
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
 
OnALearningCurveCommented:
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
 
lisa_mcAuthor Commented:
ok mark

that would be great thanks very much
0
 
lisa_mcAuthor Commented:
sorry just noticed your post will try that thanks
0
 
lisa_mcAuthor Commented:
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
 
lisa_mcAuthor Commented:
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
 
OnALearningCurveCommented:
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
 
OnALearningCurveCommented:
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
 
lisa_mcAuthor Commented:
perfect solution thank you so much for sticking with the question

appreciate it thanks
0
 
lisa_mcAuthor Commented:
again our posts crossed

thank you for the alternative
0
 
OnALearningCurveCommented:
No Problem,

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now