Thursday, April 28, 2011

Get distance between one date and another in SQL (ISO format)

Let's say I have a table with two columns, where a user places a booking.

datefrom               dateto
-------------------------------------------
2009-05-23 00:00:00    2009-05-27 00:00:00

How would I use SQL to return how many days the user will be booking for? All I want is to return the number of days.

From stackoverflow
  • select days=datediff(dd,datefrom,dateto) from table

    leppie : 1 second! :)
    EnderMB : Fantastic. Works perfectly!
  • In Oracle:

    select dateto - datefrom from table;
    
  • SQL-92:

    SELECT (dateto - datefrom) DAY
      FROM MyTable;
    

0 comments:

Post a Comment