0

I have the following snippet:

 CONVERT(varchar, FLOOR(ROUND((DATEDIFF(minute, shiftStart.timeEntered, shiftEnd.timeEntered) - DATEDIFF(minute, 
                      lunchStart.timeEntered, lunchEnd.timeEntered)) * 1.0 / 60, 2, 1))) + ':' + CONVERT(varchar, ROUND(ROUND(DATEDIFF(minute, shiftStart.timeEntered, 
                      shiftEnd.timeEntered) * 1.0 / 60, 2, 1) % 1 * 60, 0)) AS WorkingHours

The above code prints hour and minute in the following format:

4:30.000000
7:40.000000
8:1.000000

I need to rid of the . and next 0's so i get the following format:

4:30 (4 hours and 30 mins)
7:40 (7 hours and 40 minutes)
8:1 (means 8 hours and 1 minute)

.0000 Doesn't harm but it gives bad look

1 Answer 1

1

Convert the minutes portion to INTeger before converting to VARCHAR to drop the decimal values:

CONVERT(VARCHAR, 
        CONVERT(INT, ROUND(ROUND(DATEDIFF(minute, shiftStart.timeEntered, shiftEnd.timeEntered) * 1.0 / 60, 2, 1) % 1 * 60, 0))) 

Full example:

CONVERT(varchar, FLOOR(ROUND((DATEDIFF(minute, shiftStart.timeEntered, shiftEnd.timeEntered) - DATEDIFF(minute, lunchStart.timeEntered, lunchEnd.timeEntered)) * 1.0 / 60, 2, 1))) + ':' + 
CONVERT(VARCHAR, CONVERT(INT, ROUND(ROUND(DATEDIFF(minute, shiftStart.timeEntered, shiftEnd.timeEntered) * 1.0 / 60, 2, 1) % 1 * 60, 0))) AS WorkingHours
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.