Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query DATEDIFF returning NULL

Tags:

mysql

datediff

I am writing this query in the mySQL DB. The table listing has two columns StartDate & EndDate. I want to return the Duration ie number of days between these two dates using DATEDIFF. My query is:

SELECT DATEDIFF(StartDate, EndDate) as 'Duration' FROM listing;

The table returns NULL in the Duration column.

And if I write,

SELECT DATEDIFF(day, StartDate, EndDate) as 'Duration' FROM listing;

returns

Error Code: 1582. Incorrect parameter count in the call to native function 'datediff'   0.000 sec

Any help will be appreciated.

like image 257
brokenfoot Avatar asked Dec 06 '25 19:12

brokenfoot


1 Answers

The problem was that DATEDIFF() expects the dates to be in the format YYYYMMDD and the column inputs of StartDate and EndDate that I was giving was MMDDYYYY. I changed it to YYYYMMDD and it worked. So, the following expression with the dates in YYYYMMDD format works just fine.

SELECT DATEDIFF(StartDate, EndDate) as 'Duration' FROM listing;
like image 52
brokenfoot Avatar answered Dec 09 '25 13:12

brokenfoot