I need to get the sum of digits of year in format date
, for example 1997 = 1+9+9+7 = 26
. I have to do it in MySQL with SELECT
only. I’ve tried to use SUM
function, but it sums only years in a few rows.
Table name: list.
In phpMyAdmin, I’m executing the following command and getting the following results:
2
Answers
You can do arithmetics for this:
Yields:
Starting from a
date
ordatetime
value, you can useyear()
to extract the year part first:Or you can use something like this.
Result for
1997-01-01
View on DB Fiddle