Skip to content
Advertisement

How long in years the artist was in the band for all artists who are or ever have been in bands

For the artists currently in bands, the value is null. How can I calculate how long they have been in the band if the value is null using mySQL?

I was thinking of doing something like this, which only works for artists who have left a band.

truncate(datediff(m.leave_date, m.joined_date)/365, 0)

Advertisement

Answer

I haven’t done SQL for a while so please bear with me.

Basically, you want to coalesce your leave date to “now” if it’s null (current member). See https://www.w3schools.com/sql/func_mysql_ifnull.asp

truncate(datediff( 
    ifnull(m.leave_date, now()),
    m.joined_date)/365, 0)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement