某人2007年3月10日出生,怎么表达返回出生天数,与月份数(多少个月零多少天)
------解决方案--------------------
SELECT DATEDIFF(day, '2007-03-10', getdate())/30 as Months ,DATEDIFF(day, '2007-03-10', getdate())%30 as Days
------解决方案--------------------
- SQL code
declare @t datetimeselect @t='2007-3-20'select case when day(@t)<=day(getdate()) then convert(varchar(10),datediff(month,@t,getdate()))+'个月零'+convert(varchar(10),datediff(day,@t,dateadd(month,-datediff(month,@t,getdate()),getdate())))+'天' else convert(varchar(10),datediff(month,@t,getdate())-1)+'个月零'+convert(varchar(10),datediff(day,dateadd(month,datediff(month,@t,getdate())-1,@t),getdate()))+'天' end