Calculating the difference between two dates in MySQL database.

vishalneeraj-24503
Posted by vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 317
Calculating difference between two dates in MySQL is really easy.
Here's how we can do it.Let's say DOB1 and DOB2 are 2 columns in table table_name.
Here,we will use UNIX_TIMESTAMP as in-built MySql Database function.
At Column Level:-
SELECT UNIX_TIMESTAMP(DOB2) - UNIX_TIMESTAMP(DOB1) from table_name;


At Variable Level:-
DECLARE @DOB1 AS DATETIME;
DECLARE @DOB2 AS DATETIME;
SET @DOB1 = '2014-11-09 00:00:00';
SET @DOB2 = '2014-10-07 00:00:00';
SELECT UNIX_TIMESTAMP(@DOB2) - UNIX_TIMESTAMP(@DOB1);

Comments or Responses

Login to post response