Click here to Skip to main content
15,076,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I hava problem about query


i want to get difference between two hours


example

SELECT DATEDIFF(HOUR, '09:00:00', '10:00:00') AS [Durée en Seconds]


the result is '1'

but when i use this query
SELECT DATEDIFF(HOUR, '10:00:00', '09:00:00') AS [Durée en Seconds]


What I have tried:

How can i get the difference hour ,always in positive value

I wish the result in seconde query is '1'

How can i solve this?
Posted
Updated 8-Jun-20 1:34am

Quote:
How can i get the difference hour ,always in positive value

Did you try ABS() (absolute value function) ?
   
Comments
Member 14760154 8-Jun-20 1:49am
   
yes i did,and it succes,

now problem is solved
Patrice T 8-Jun-20 2:01am
   
You can also accept the solution, it will close the question and say everyone that question is slved.
CPallini 8-Jun-20 2:15am
   
5.
Patrice T 8-Jun-20 2:16am
   
Thank you
MadMyche 8-Jun-20 10:24am
   
+5, and referenced in my answer
Patrice T 8-Jun-20 13:25pm
   
Thank you
If you read the documentation for the DateDiff[^] function, you will see that the usage is
DATEDIFF ( datepart , startdate , enddate )

If the StartDate is before the EndDate, the result will be a positive number.
If the StartDate occurred after EndDate, the result will be a negative.

If you always want a positive number, then you use the Abs[^] function as outlined by Patrice
And if you always want a negative number; then you use the Abs function and multiply by -1.

References:
DATEDIFF (Transact-SQL) - SQL Server | Microsoft Docs[^]
ABS (Transact-SQL) - SQL Server | Microsoft Docs[^]
   

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