Question

I have one table which is having two columns as a DATETIME like following

CreatedDate                 DeliveryDate                Diff_Date
2013-05-12 18:30:00.000     2013-05-17 18:30:00.000     1900-01-06 00:00:00.000

I want Diff_Date for that i did sql query like

select (DeliveryDate -  CreatedDate) as  Diff_Date from MYTABLE

but i'm getting wrong answer :(

please correct me.

Was it helpful?

Solution 2

Try this:

For MySQL DATEDIFF() frinction.

SELECT DATEDIFF(DeliveryDate, CreatedDate) AS Diff_Date FROM MYTABLE

For SQL SERVER DATEDIFF() function

SELECT DATEDIFF(dd, CreatedDate, DeliveryDate) AS Diff_Date FROM MYTABLE

OTHER TIPS

If you want the number of days as an integer between two values then use:

select datediff(day, DeliveryDate, CreatedDate)

You can try DATEDIFF function.

http://www.w3schools.com/sql/func_datediff.asp

Use

Select Datediff(DD, SmallDate,BigDate).

try like this:

SELECT DATEDIFF(DeliveryDate ,CreatedDate) as  Diff_Date from MYTABLE

For more information you can refer to documentation :http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_datediff

    declare @CreatedDate datetime                              
    set @CreatedDate='2013-05-12 18:30:00.000'          
    declare @DeliveryDate datetime
    set @DeliveryDate='2013-05-17 18:30:00.000'

    declare @Diff_Date datetime
    set @Diff_Date='1900-01-06 00:00:00.000'

    --select Datediff(d,@CreatedDate,@DeliveryDate)
    select cast((@DeliveryDate-@CreatedDate) as datetime)

it returns same results as date_diff
-- 1900-01-06 00:00:00.000

fiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top