我有这个功能,显示日期之间的年,月和日。在视图表上,我有包含开始和结束日期的数据。如何将该功能集成到我的视图表中?
USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter FUNCTION [db].[GetDateDifference]
(
@FromDate DATETIME, @ToDate DATETIME
)
RETURNS
@DateDifference TABLE (
YEAR INT, MONTH INT, DAYS INT)
AS
BEGIN
DECLARE @Years INT, @Months INT, @Days INT, @tmpFromDate DATETIME
SET @Years = DATEDIFF(YEAR, @FromDate, @ToDate)
- (CASE WHEN DATEADD(YEAR, DATEDIFF(YEAR, @FromDate, @ToDate),
@FromDate) > @ToDate THEN 1 ELSE 0 END)
SET @tmpFromDate = DATEADD(YEAR, @Years , @FromDate)
SET @Months = DATEDIFF(MONTH, @tmpFromDate, @ToDate)
- (CASE WHEN DATEADD(MONTH,DATEDIFF(MONTH, @tmpFromDate, @ToDate),
@tmpFromDate) > @ToDate THEN 1 ELSE 0 END)
SET @tmpFromDate = DATEADD(MONTH, @Months , @tmpFromDate)
SET @Days = DATEDIFF(DAY, @tmpFromDate, @ToDate)
- (CASE WHEN DATEADD(DAY, DATEDIFF(DAY, @tmpFromDate, @ToDate),
@tmpFromDate) > @ToDate THEN 1 ELSE 0 END)
INSERT INTO @DateDifference
VALUES(@Years, @Months, @Days)
RETURN
END
这是我的观察表:
USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter VIEW [db].[View_ServiceTickets]
AS
SELECT TOP 99
ServiceTicketDate,
BillingDate as 'Project Finished',
InspectionScheduleDate
FROM tblServiceTicket INNER JOIN
tblServiceTicketReport on tblServiceTicket.ServiceTicketID = tblServiceTicketReport.ServiceTicketID
where ServiceTicketDate > '2016'
order by ServiceTicketDate desc
GO
非常感谢
答案 0 :(得分:0)
我看到你的函数的返回数据类型是一个表,所以你使用它作为一个表。 你可以测试以下内容:
alter VIEW [db].[View_ServiceTickets]
AS
SELECT TOP 99
ServiceTicketDate,
BillingDate as 'Project Finished',
InspectionScheduleDate,
d.*
FROM tblServiceTicket INNER JOIN
tblServiceTicketReport on tblServiceTicket.ServiceTicketID = tblServiceTicketReport.ServiceTicketID
OUTER APPLY(SELECT YEAR,MONTH,DAYS FROM [db].[GetDateDifference](ServiceTicketDate,BillingDate)) d
where ServiceTicketDate > '2016'
order by ServiceTicketDate desc