Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
how to count total number of hours and days from date and time in sql querry?


My table Is-


Id CheckInDate   CheckInTime     CheckOutDate    CheckOutTime

1  2014-12-28   09 : 29 : AM    2014-12-30  06 : 29 : AM

2  2014-12-27   11 : 30 : AM    2014-12-30  12 : 30 : PM



Please solve this problem?
Posted

1 solution

Change your database.
Stop storing dates and times as NVARCHAR, and stop storing them separately. Always store dates and times together, so they form a single "moment in time" in DATETIME variables.
Then you can just use the DATEDIFF function[^] which returns what you want.

If you store them as strings, you have to validate and convert them every single time you want to use them, and that's bad. A single error that manages to get into your DB will mess up all your queries.
 
Share this answer
 
Comments
King Fisher 28-Dec-14 5:56am    
Well said 5+ ;)

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