SQL Server SSRS Expression Not Adding 18 Months Correctly to a Date

2cmtqfgy  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(112)

In one of my reports I have this query expression.

=iif(isdate(Fields!Date_of_Funding_Approval.Value),iif(isdate(Fields!Extension_Expiration_Date.Value),Fields!Extension_Expiration_Date.Value,DateAdd("m",18,Fields!Date_of_Funding_Approval.Value)),"")

Date_of_Funding_Approval is a valid date = 04/24/2019

Extension_Expiration_Date is null

So, I am trying to add 18 months to Date_of_Funding_Approval, which should equal 10/24/2020 but it is returning 04/24/2025.

UPDATE

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Master Table](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [New Project ID#] [nvarchar](255) NULL,
    [District Name] [nvarchar](255) NULL,
    [Program] [nvarchar](50) NULL,
    [School Name] [nvarchar](255) NULL,
    [Project Description] [nvarchar](255) NULL,
    [Qualifying Project Costs] [money] NULL,
    [Wealth Index] [float] NULL,
    [State Financial Participation] [money] NULL,
    [Notes] [nvarchar](max) NULL,
    [Means Value] [float] NULL,
    [Division Reviewed Project Size/POR] [int] NULL,
    [Primary Category] [nvarchar](50) NULL,
    [Secondary Category] [nvarchar](50) NULL,
    [Means Cost Center] [nvarchar](150) NULL,
    [Rescinded] [nvarchar](150) NULL,
    [Final] [nvarchar](150) NULL,
    [Green Incentive SFP] [money] NULL,
    [Date Printed] [datetime] NULL,
    [Manager Area] [nvarchar](255) NULL,
    [Grand Total SFP] [money] NULL,
    [Last_Updated] [datetime] NULL,
    [Extension Expiration Date] [datetime] NULL,
    [Extension Granted] [bit] NOT NULL,
    [Date of Funding Approval] [datetime] NULL,
    [Project Year] [nvarchar](255) NULL,
    [Final Cost SFP of Project] [money] NULL,
    [Project Shown in CMMS] [bit] NOT NULL,
    [Final Pay Inspection Name] [nvarchar](255) NULL,
    [Final Pay Inspection Date] [datetime] NULL,
    [Date Entered] [datetime] NULL,
    [Date Inspected] [datetime] NULL,
    [Percent Complete] [int] NULL,
    [Manager Name] [nvarchar](255) NULL,
    [Date of Report] [datetime] NULL,
    [Additional Notes] [ntext] NULL,
    [LEA] [int] NULL,
    [Stat Financial Participation Notes] [ntext] NULL,
    [username] [nvarchar](50) NULL,
    [conflict_timestamp] [timestamp] NULL,
 CONSTRAINT [PK_Master_Table] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Master Table] ON 
GO
INSERT [dbo].[Master Table] ([ID], [New Project ID#], [District Name], [Program], [School Name], [Project Description], [Qualifying Project Costs], [Wealth Index], [State Financial Participation], [Notes], [Means Value], [Division Reviewed Project Size/POR], [Primary Category], [Secondary Category], [Means Cost Center], [Rescinded], [Final], [Green Incentive SFP], [Date Printed], [Manager Area], [Grand Total SFP], [Last_Updated], [Extension Expiration Date], [Extension Granted], [Date of Funding Approval], [Project Year], [Final Cost SFP of Project], [Project Shown in CMMS], [Final Pay Inspection Name], [Final Pay Inspection Date], [Date Entered], [Date Inspected], [Percent Complete], [Manager Name], [Date of Report], [Additional Notes], [LEA], [Stat Financial Participation Notes], [username]) VALUES (3350, N'xxx-7207-010', N'xxx School District', N'xxx Partnership', N'-Not Specified-', N'xxx - Additions and Conversions', 7022599.0800, 0.43051886092115882, 3999237.7234, NULL, 153.58, 45726, N'Space', N'Conversion + Addition', N'xxx', NULL, NULL, NULL, CAST(N'2022-02-16T07:36:47.000' AS DateTime), N'1', 3999237.7234, CAST(N'2022-02-16T07:36:47.000' AS DateTime), NULL, 0, CAST(N'2019-04-24T00:00:00.000' AS DateTime), N'Year 1', NULL, 0, NULL, NULL, CAST(N'2019-04-29T00:00:00.000' AS DateTime), NULL, NULL, NULL, NULL, NULL, 7207, NULL, NULL)
GO
SET IDENTITY_INSERT [dbo].[Master Table] OFF
GO
ALTER TABLE [dbo].[Master Table] ADD  CONSTRAINT [DF__Master Ta__Means__38845C1C]  DEFAULT ((0)) FOR [Means Value]
GO
ALTER TABLE [dbo].[Master Table] ADD  CONSTRAINT [DF__Master Ta__Divis__39788055]  DEFAULT ((0)) FOR [Division Reviewed Project Size/POR]
GO
ALTER TABLE [dbo].[Master Table] ADD  CONSTRAINT [DF__Master Ta__Green__3A6CA48E]  DEFAULT ((0)) FOR [Green Incentive SFP]
GO
ALTER TABLE [dbo].[Master Table] ADD  CONSTRAINT [DF__Master Ta__Exten__3B60C8C7]  DEFAULT ((0)) FOR [Extension Granted]
GO
ALTER TABLE [dbo].[Master Table] ADD  CONSTRAINT [DF__Master Ta__Proje__3C54ED00]  DEFAULT ((0)) FOR [Project Shown in CMMS]
GO
nimxete2

nimxete21#

Try using DateSerial within the query to add the 18 months, instead of DateAdd

It is not really any different to query in the question, except with DataSerial , it will allows to specify the year, month, and day components explicitly.

So the altered expression will be:

=iif(
  IsDate(Fields!Date_of_Funding_Approval.Value),
  iif(
    IsDate(Fields!Extension_Expiration_Date.Value),
    Fields!Extension_Expiration_Date.Value,
    DateSerial(
      Year(Fields!Date_of_Funding_Approval.Value),
      Month(Fields!Date_of_Funding_Approval.Value) + 18,
      Day(Fields!Date_of_Funding_Approval.Value)
    )
  ),
  ""
)

相关问题