0

Conversion failed when converting the varchar value 'Hours' to data type int. This is my query:

Sum(Cast(Cast(DATEDIFF(HH, ld_time_tracker_start_time,
 fld_time_tracker_end_time)%3600/60 As int) +' Hours'+ 
Cast(DATEDIFF(MINUTE, ld_time_tracker_start_time,
 fld_time_tracker_end_time)As int )+'Minutes' As Int)) 
As [Time Spent(In hrs)]

I modified the above Code To:

+Cast('Hours' As Int)
+Covert(int,'Hours')

... and got the error message:

Conversion failed when converting the varchar value ' Hours' to data type int

PhilTM
32k10 gold badges86 silver badges108 bronze badges
asked Jan 27, 2016 at 6:23

1 Answer 1

2

This is not clear what output is needed. You are trying to convert text strings to int. This obviously does not work. Besides this type of formatting might fit better outside of SQL Server.

If you want something like 5 Hours 15 Minutes you can try this query:

[Time Spent(In hrs)] =
 CAST(
 SUM(
 DATEDIFF(hour, ld_time_tracker_start_time, ld_time_tracker_end_time) 
 )
 as varchar(10))
 +' Hours '+ 
 CAST(
 SUM(
 DATEDIFF(minute, ld_time_tracker_start_time, ld_time_tracker_end_time) % 60 
 )
 as varchar(10))
+' Minutes'

Note that I have replaced HH by hour. This is easier to read and understand and it avoid mistakes. See DATEDIFF (Transact-SQL).

answered Jan 27, 2016 at 7:02
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.