Home > database >  SQL query entry date, how to convert to and as of today, a few years a few months
SQL query entry date, how to convert to and as of today, a few years a few months

Time:11-09

SQL query entry date, how to convert to as of today, a few years a few months (only know entry date)
Such as
Entry date today, date display the results () with the date and are fixed number of year (year)

2010/9/17 2020/08/22 nine years 11 months
2011/2/22 2020/08/22 9 years and 6 months
5 years and 8 months 2014/12/2 2020/08/22
2016/3/4 2020/08/22 4 years and 5 months
2018/12/29 2020/08/22 1 year 7 months




CodePudding user response:

 create table # temp (inter_date datetime) 

Insert into # temp values (' 2010-09-17 ')
Insert into # temp values (' 2011-02-22 ')
Insert into # temp values (' 2014-12-02 ')
Insert into # temp values (' 2016-03-04 ')


The select inter_date, GETDATE (), ((case DATEDIFF (mm, inter_date, GETDATE ())/12 when 0 then 'else convert (varchar, DATEDIFF (mm, inter_date, GETDATE ())/12) +' years' end)
+ (case DATEDIFF (mm, inter_date, GETDATE ()) % 12 when 0 then 'else convert (varchar, DATEDIFF (mm, inter_date, GETDATE ()) % 12) +' months 'end)) from # temp


Drop table # temp



Can consider function occurred

CodePudding user response:

Upstairs is solution! For the year and month of the entry
  • Related