我正在使用注册证书,该证书需要婴儿的年龄,可能是数年,数月或数天。我使用了= DateDiff("YYYY",Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value)
,但是当我将其更改为月份或日期时,它只生成相同的年份。我想要的是它会计算孩子的年龄,无论是一个月还是几天。
答案 0 :(得分:3)
如果有效,请尝试下面的t-sql代码:
CREATE TABLE dbo.RegistrationCertificate (IssuedOnORNo DATETIME, CertificationDate DATETIME)
INSERT dbo.RegistrationCertificate (IssuedOnORNo, CertificationDate)
SELECT GETDATE(), DATEADD(MONTH, 13, GETDATE())
UNION ALL
SELECT GETDATE(), GETDATE() + 1
UNION ALL
SELECT GETDATE() - 2, GETDATE()
UNION ALL
SELECT GETDATE(), DATEADD(YEAR, 3, GETDATE())
UNION ALL
SELECT GETDATE(), DATEADD(MONTH, 4, GETDATE())
SELECT t.*
, YEARS = DATEDIFF(YEAR, t.IssuedOnORNo, t.CertificationDate)
, MONTHS = (DATEDIFF(MONTH, t.IssuedOnORNo, t.CertificationDate) - ((DATEDIFF(MONTH, t.IssuedOnORNo, t.CertificationDate) / 12) * 12))
, DAYS = ((DATEDIFF(DAY, t.IssuedOnORNo, t.CertificationDate) - ((DATEDIFF(DAY, t.IssuedOnORNo, t.CertificationDate) / 365) * 365))
- (((DATEDIFF(MONTH, t.IssuedOnORNo, t.CertificationDate) - ((DATEDIFF(MONTH, t.IssuedOnORNo, t.CertificationDate) / 12) * 12))) * 30) - 1)
FROM dbo.RegistrationCertificate t WITH(NOLOCK)
btw roger,如果您想知道如何在报告中使用该代码,您可以使用上面的代码为报告数据创建存储过程。
编辑:
在这种情况下,下面的代码应该足够接近您的报告代码:
YEARS = DateDiff("YYYY",Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value)
MONTHS = (DateDiff("m", Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value)
- ((DateDiff("m", Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value) / 12) * 12))
DAYS = ((DateDiff("dd", Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value) - ((DateDiff("dd", Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value) / 365) * 365))
- (((DateDiff("m", Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value) - ((DateDiff("m", Fields!IssuedOnORNo.Value, Fields!CertificationDate.Value) / 12) * 12))) * 30) - 1)