我使用以下语法来区分两个日期:
DATEDIFF(minute, [Dam_Date_Of_Birth], [Calf_Date_Of_Birth]) as [minutes]
此查询给出了以下结果:
minutes
1748160
3711420
768960
4231380
5816779
如何将这些值变为年,月,日格式?
答案 0 :(得分:3)
你也可以这样做
DECLARE @diff float = 1748160 / 24.0 / 60.0
Declare @date datetime = convert(datetime, @diff);
Select DatePart(yy, @date) - 1900 as Years, DatePart(mm, @date) - 1 Months, DatePart(dd, @diff) - 1 Days
或者您只需将您的会议记录添加到1900年1月1日'得到相同的结果:
Declare @date datetime = DateAdd(minute, 1748160, '1 Jan 1900');
Select DatePart(yy, @date) - 1900 as Years, DatePart(mm, @date) - 1 Months, DatePart(dd, @date) - 1 Days
所以你可以这样使用:
DECLARE @dtStartDate date = '01 Jan 2015'
DECLARE @dtEndDate date = '11 Jan 2015'
Declare @date datetime = DateAdd(minute, DATEDIFF(minute, @dtStartDate, @dtEndDate), '1 Jan 1900');
Select DatePart(yy, @date) - 1900 as Years, DatePart(mm, @date) - 1 Months, DatePart(dd, @date) - 1 Days
答案 1 :(得分:0)
参考this
DECLARE @nMonth INT, @nYear INT, @mm INT, @dd INT, @getdd int
DECLARE @dtStartDate AS DATETIME, @dtEndDate AS datetime
SET @dtStartDate = '01/01/2015'
SET @dtEndDate = '02/11/2015'
SET @nYear = DATEDIFF(yy, @dtStartDate, @dtEndDate)
SET @mm = DATEDIFF(mm, @dtStartDate, @dtEndDate)
SET @dd = DATEDIFF(dd, @dtStartDate, @dtEndDate)
SET @nMonth = ABS(DATEDIFF(mm, DATEADD(yy, @nYear, @dtStartDate), @dtEndDate))
SET @getdd = ABS(DATEDIFF(dd,
DATEADD(mm,
DATEDIFF(mm,
DATEADD(yy, @nYear, @dtStartDate),
@dtEndDate),
DATEADD(yy, @nYear, @dtStartDate)),
@dtEndDate))
SELECT Convert(varchar(10),@nYear) + ' year ' +
Convert(varchar(10),@nMonth) + ' month ' +
Convert(varchar(10),@getdd) + ' day(s)'
输出: 0 year 1 month 10 day(s)
答案 2 :(得分:0)
解决问题366天:
declare @st_date date = '2318-01-01';
declare @end_date date = '2417-12-31';
select datepart(yyyy,dateadd(year,(year(@st_date))%4+((year(@st_date)+100)%400/100)*100,cast(0 as datetime)) + datediff(day,@st_date,@end_date) ) -
(1900+(year(@st_date))%4+(((year(@st_date)+100)%400/100)*100)) as year
,datepart(mm,dateadd(year,(year(@st_date))%4+((year(@st_date)+100)%400/100)*100,cast(0 as datetime)) + datediff(day,@st_date,@end_date) ) - 1 as month
,datepart(dd,dateadd(year,(year(@st_date))%4+((year(@st_date)+100)%400/100)*100,cast(0 as datetime)) + datediff(day,@st_date,@end_date) ) - 1 as day
输出 99 year 11 month 30 day
总月份:
select (datepart(yyyy,dateadd(year,(year(@st_date))%4+((year(@st_date)+100)%400/100)*100,cast(0 as datetime)) +
datediff(day,@st_date,@end_date) ) - (1900+(year(@st_date))%4+(((year(@st_date)+100)%400/100)*100))) * 12 +
datepart(mm,dateadd(year,(year(@st_date))%4+((year(@st_date)+100)%400/100)*100,cast(0 as datetime)) +
datediff(day,@st_date,@end_date) ) - 1 as totalmonth
输出 1199 totalmonth
答案 3 :(得分:-1)
更新:
DECLARE
@yearMin DATE = '1983.01.01' ,
@yearMax DATE = '2014.12.23' ,
@years INT ,
@months INT ,
@days INT;
SELECT
@years = DATEDIFF(YEAR, @yearMin, @yearMax) ,
@months = DATEDIFF(MONTH, @yearMin, @yearMax) - @years * 12 ,
@days = DATEDIFF(DAY, @yearMin,
DATEADD(MONTH, -@months,
DATEADD(YEAR, -@years, @yearMax)))
SELECT
@years AS Years ,
@months AS Months ,
@days AS Days;