Home > Blockchain >  SQL converting month into Years and Months
SQL converting month into Years and Months

Time:07-15

I have an X number of months, but I need to break it into xx years yy months in SQL.

For example: 31 months should be 2 years 7 months.

Is there any inbuilt function or something to help?

Many thanks for your help.

CodePudding user response:

You can use modulus to get the months and floor to get the years

 SELECT totalmonths, FLOOR(tottalmonths / 12) AS years, totalmonths % 12 as months
 FROM -- ...

CodePudding user response:

You can try this:

     SELECT 
       (months_column_name / 12) AS years, 
       (months_column_name % 12) AS months
       
  FROM column_name
  • Related