Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I execute

SELECT datepart(HH,'24:31')

following error occurs

Conversion failed when converting date and/or time from character string. Severity 16


How I get hours in 24 and above

Thanks In advance

What I have tried:

Please Its Urgnet

SELECT datepart(HH,'24:31')
Posted
Updated 2-Apr-18 22:46pm

There's no such thing as hour 24. You should use 00.

Time is measured from 00:00 (midnight) to 23:59 (11:59pm)
 
Share this answer
 
v2
Comments
shoaib Naeem 3-Apr-18 4:54am    
Accutally its working Hours of and Employee and I want to convert it into minutes
24:31 is not a valid time: SQL datetime values (which DATEPART expects) cannot represent it as a time so you correctly get the error - which you do not with just a valid time:
SELECT datepart(HH,'23:31')

You will have to look at exactly where the string came from, and work out from that what you can do with it.

We can't suggest anything - we have no idea where you get the value '24:31' from, but the chances are that is comes from your DB and is badly stored - you should never store anything other than true strings as VARCHAR or NVARCHAR - dates, times, numbers: all need to be stored in "proper" columns, not strings. Probably, you need to store it as a number of minutes in an INT instead of string based hours and minutes.
 
Share this answer
 
Comments
shoaib Naeem 3-Apr-18 4:54am    
Accutally its working Hours of and Employee and I want to convert it into minutes
OriginalGriff 3-Apr-18 4:56am    
So store it as minutes.
Don't mess about trying to get SQL to sort it out - you can do it but it's messy as SQL's string handling is poor - convert it to minutes in your presentation software and store that directly.
shoaib Naeem 3-Apr-18 5:03am    
ok Thanks
OriginalGriff 3-Apr-18 5:07am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900