Search code examples
mysqldatediff

Datediff in columns in MySQL


I am wanting to use the datediff function in mySQL but my code its returning Nulls and I think its because the columns that I am using have a format of MM/DD/YYYY. How could I over come this. This is the code that I have.

select p.`Brand.Name`, datediff( 'p.`Pdo.Date`', 'f.`EntryDate`') from packaging as p
join volumes as v using(batchNum)
join fermentation as f on v.`LineNum` = f.`QC.LineId` 
group by `Brand.Name`;

Solution

  • Use STR_TO_DATE to convert your text to a date

    select p.`Brand.Name`, datediff( STR_TO_DATE(`p`.`Pdo.Date`,"%m/%d/%Y"), STR_TO_DATE(`f`.`EntryDate`,"%m/%d/%Y")) 
    from packaging as p
    join volumes as v using(batchNum)
    join fermentation as f on v.`LineNum` = f.`QC.LineId` 
    group by `Brand.Name`;