chiranth virat
chiranth virat

Reputation: 1

Sql date difference query

I need to take diffrence of two dates which are columns in my table: LastAIResponseReceivedDate,LastUploadedDate

I am Using this statement:

update Group set AITime=(CONVERT (varchar,(LastAIResponseReceivedDate-LastUploadedDate),121)) where GroupId=2 and Id=5

I am getting the time diffrence correctly but my date diffrence is wrong 1900-01-01 01:56:56.427

How to get the date diffrence correctly

Upvotes: 0

Views: 125

Answers (2)

MatrixTXT
MatrixTXT

Reputation: 2502

Parse to datetime first, you need

Set timediff = DATEDIFF(s, LastAIResponseReceivedDate,LastUploadedDate)

Save time difference as second is more convenient for later use. If you want to save in some date format, remember not datetime, time difference is not a 'datetime'. So,

Select CONVERT(varchar, FLOOR(timediff/(3600*24))) + 'D ' + CONVERT(varchar, DATEADD(s, timediff, 0), 114)

Upvotes: 0

Irfan
Irfan

Reputation: 695

Do not understand in your question, that what type of difference you want (Day, month, year). But this may help you.

SELECT DATEDIFF(month, 'Start_date_variable', 'End_date_variable') AS DateDiff;

Syntax As:

DATEDIFF(interval, date1, date2)

Interval can be anything like day, month, year, week, hours, second etc..

Upvotes: 1

Related Questions