Arithmetic overflow issue after executing stored procedure

I have issue with one script , the error is the following:

ERROR: Arithmetic overflow error converting expression to data type int.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[PCO_PERF_REPORT]
@IN_DATE VARCHAR(8) = NULL
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

DECLARE @version_script varchar(200)
SET @version_script = ‘StoredProcedure [dbo].[PCO_PERF_REPORT] v1.3 2021-03-17’

DECLARE @cdate date
IF (@IN_DATE IS NULL)
SET @cdate = GETDATE()
ELSE
BEGIN
SET @cdate = (SELECT convert(date, @IN_DATE))
IF (@cdate IS NULL)
BEGIN
print ‘Input date wrong format YYYYMMDD: ‘ + @IN_DATE
RETURN
END
END


Last 12 Month’s Number of Applications per Month ———————————————–


SELECT TOP 12
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,COUNT([ResponseTime].[ApplicationID]) AS ‘Total Apps’
FROM [dbo].[ResponseTime]

WHERE [dbo].[ResponseTime].[ServiceID] IN (’01’,’02’,’03’) –To Remove internal interfaces

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC


Last Month’s Number of Applications per Day —————————————————-


SELECT
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) AS ‘Date’
,COUNT([ResponseTime].[ApplicationID]) AS ‘Total Apps’
FROM [dbo].[ResponseTime]

WHERE SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) >= DATEADD(M,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –start of last month (1st)
AND SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) <= DATEADD(S,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –end of last month (last day)
AND [dbo].[ResponseTime].[ServiceID] IN (’01’,’02’,’03’) –To Remove internal interfaces

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8)


Last Month’s Number of Applications per Hour per Day ——————————————-


SELECT
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) AS ‘Date’
,SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),9,2)+’:00 > ‘+CAST(CAST(SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),9,2) AS INT)+1 AS VARCHAR)+’:00′ AS ‘Hour’
,COUNT([ResponseTime].[ApplicationID]) AS ‘Total Apps’
FROM [dbo].[ResponseTime]

WHERE SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) >= DATEADD(M,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –start of last month (1st)
AND SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) <= DATEADD(S,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –end of last month (last day)
AND [dbo].[ResponseTime].[ServiceID] IN (’01’,’02’,’03’) –To Remove internal interfaces

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8), SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),9,2)


Last 12 Month’s Enrichment Interface Response Times per Month (Badexcug) ————————-


SELECT TOP 12
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,ROUND(AVG([Duration]), 2) AS ‘Badexcug Call Duration’

FROM [dbo].[ResponseTime]

WHERE [ResponseTime].[ServiceID] = ‘BadexcugInterfacePack’

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC

Last 12 Month’s Enrichment Interface Response Times per Month ASNEF Detail ————————-


SELECT TOP 12
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,ROUND(AVG([Duration]), 2) AS ‘ASNEF Detail Call Duration’

FROM [dbo].[ResponseTime]

WHERE [ResponseTime].[ServiceID] = ‘ASNEF_Detail_IP’

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC

Last 12 Month’s Enrichment Interface Response Times per Month RiskScore & Severity ————————-


SELECT TOP 12
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,ROUND(AVG([Duration]), 2) AS ‘RiskScore and Severity Call Duration’

FROM [dbo].[ResponseTime]

WHERE [ResponseTime].[ServiceID] = ‘RiskScoreIndividualsAndSeverity_IP’

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC

Last 12 Month’s Enrichment Interface Response Times per Month (Gas Natural) ————————-


SELECT TOP 12
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,ROUND(AVG([Duration]), 2) AS ‘Gas Natural Call Duration’

FROM [dbo].[ResponseTime]

WHERE [ResponseTime].[ServiceID] = ‘GasNaturalInterfacePack’

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC

Last 12 Month’s Enrichment Interface Response Times per Month Incidencias Judiciales ———–


SELECT TOP 12
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,ROUND(AVG([Duration]), 2) AS ‘Incidencias Judiciales Call Duration’

FROM [dbo].[ResponseTime]

WHERE [ResponseTime].[ServiceID] = ‘Equifax_Judiciales_IP’

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC

Last 12 Month’s Enrichment Interface Response Times per Month Modelo R ———–


SELECT TOP 12
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,ROUND(AVG([Duration]), 2) AS ‘Modelo R Call Duration’

FROM [dbo].[ResponseTime]

WHERE [ResponseTime].[ServiceID] = ‘Modelo_R_IP’

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC

Last 12 Month’s Evaluacion Service Response Times per Month ————————————


SELECT TOP 13
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,COUNT([ResponseTime].[ApplicationID]) AS ‘Total Evaluacion Apps’
,AVG([ResponseTime].[Duration]) AS ‘bigint’
FROM [dbo].[ResponseTime]

WHERE [dbo].[ResponseTime].[ServiceID] IN (’01’,’02’) –To Remove Decision Final apps

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC

Last 12 Month’s Decision Final Service Response Times per Month ————————————


SELECT TOP 13
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) AS ‘Month’
,COUNT([ResponseTime].[ApplicationID]) AS ‘Total Decision Final Apps’
,AVG([ResponseTime].[Duration]) AS ‘bigint’
FROM [dbo].[ResponseTime]

WHERE [dbo].[ResponseTime].[ServiceID] = ’03’ –To Remove Decision Final apps

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6)
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,6) DESC


Last Month’s Number of Total Calls per Day (Badexug and Asnef) ——————————–


SELECT
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) AS ‘Date’,
CONVERT(VARCHAR(10),replace (REPLACE(ErrorCode,’1′,’Fail’),’0′,’Correct’)) as ErrorCode,ServiceID
,COUNT([ResponseTime].[ApplicationID]) AS ‘Total Apps BADEXCUG, ASNEF Detail, RiskScore & Severity, Gas Natural, Incidencias Judiciales and Modelo R Calls’
FROM [dbo].[ResponseTime]

WHERE SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) >= DATEADD(M,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –start of last month (1st)
AND SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) <= DATEADD(S,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –end of last month (last day)
AND [dbo].[ResponseTime].[ServiceID] IN (‘BadexcugInterfacePack’, ‘ASNEF_Detail_IP’, ‘RiskScoreIndividualsAndSeverity_IP’, ‘GasNaturalInterfacePack’, ‘Equifax_Judiciales_IP’, ‘Modelo_R_IP’) –AND [dbo].[ResponseTime].[ErrorCode] IN (‘1’)–To Remove internal interfaces

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8),ErrorCode, ServiceID
ORDER BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8)


Last Month’s Number of Total Calls per Day ——————————————-


–table for storing all potential missing combinations, built dynamically for every single month
DECLARE @pivotMonthTable TABLE(pivotDate varchar(8), pivotErrorCode varchar(100), pivotServiceId varchar(100))

DECLARE @monthStartDay datetime
DECLARE @monthEndDay datetime

–calculate both first day and last day of current month
set @monthStartDay = DATEADD(month,0, DATEADD(month, DATEDIFF(month, 0, DATEADD(month,-1, @cdate)), 0))
set @monthEndDay = DATEADD(month,0,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,DATEADD(month,-1, @cdate))+1,0)))

–debug
–select @monthStartDay, @monthEndDay

–populate the temp table with all month days with all potential missing combinations (day, errorcode and serviceid)
WHILE(@monthStartDay < @monthEndDay)
BEGIN
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Correct’, ‘BadexcugInterfacePack’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Fail’, ‘BadexcugInterfacePack’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Correct’, ‘ASNEF_Detail_IP’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Fail’, ‘ASNEF_Detail_IP’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Correct’, ‘RiskScoreIndividualsAndSeverity_IP’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Fail’, ‘RiskScoreIndividualsAndSeverity_IP’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Correct’, ‘GasNaturalInterfacePack’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Fail’, ‘GasNaturalInterfacePack’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Correct’, ‘Equifax_Judiciales_IP’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Fail’, ‘Equifax_Judiciales_IP’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Correct’, ‘Modelo_R_IP’
INSERT INTO @pivotMonthTable
SELECT convert(varchar(8),@monthStartDay,112), ‘Fail’, ‘Modelo_R_IP’
SELECT @monthStartDay = DATEADD(day, 1,@monthStartDay)
END

–join from prod log table and temp pivot table, keeping existing records and filling the missing records in prod log table with the temp pivot table ones
select isnull(q2.Date, pivotMonthTable.pivotDate) as ‘Date’, isnull (q2.ErrorCode,pivotMonthTable.pivotErrorCode) as ‘ErrorCode’,
isnull (q2.ServiceID, pivotMonthTable.pivotServiceId) as ‘ServiceID’, isnull (q2.[Total Apps BADEXCUG, ASNEF Detail, RiskScore & Severity, Gas Natural, Incidencias Judiciales and Modelo R Calls], ‘00000’) as ‘Total Apps BADEXCUG, ASNEF Detail, RiskScore & Severity, Gas Natural, Incidencias Judiciales and Modelo R Calls’
from @pivotMonthTable pivotMonthTable
left join (
SELECT
SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) AS ‘Date’,
CONVERT(VARCHAR(10),replace (REPLACE(ErrorCode,’1′,’Fail’),’0′,’Correct’)) as ErrorCode,[ResponseTime].ServiceID as ServiceID,
right(‘00000’ + CONVERT(VARCHAR(5),COUNT([ResponseTime].[ApplicationID])),5) AS ‘Total Apps BADEXCUG, ASNEF Detail, RiskScore & Severity, Gas Natural, Incidencias Judiciales and Modelo R Calls’
FROM [dbo].[ResponseTime]

WHERE SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) >= DATEADD(M,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –start of last month (1st)
AND SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8) <= DATEADD(S,-1,DATEADD(MM, DATEDIFF(M,0,@cdate),0)) –end of last month (last day)
AND [dbo].[ResponseTime].[ServiceID] IN (‘BadexcugInterfacePack’, ‘ASNEF_Detail_IP’, ‘RiskScoreIndividualsAndSeverity_IP’, ‘GasNaturalInterfacePack’, ‘Equifax_Judiciales_IP’, ‘Modelo_R_IP’) –AND [dbo].[ResponseTime].[ErrorCode] IN (‘1’)–To Remove internal interfaces

GROUP BY SUBSTRING(CONVERT(VARCHAR, [ResponseTime].[StartTime], 23),1,8),
CONVERT(VARCHAR(10),replace (REPLACE(ErrorCode,’1′,’Fail’),’0′,’Correct’)),
[ResponseTime].ServiceID) as q2
on (pivotMonthTable.pivotDate = q2.Date and pivotMonthTable.pivotErrorCode = q2.ErrorCode and pivotMonthTable.pivotServiceId = q2.ServiceID)
ORDER BY isnull(q2.Date, pivotMonthTable.pivotDate), isnull (q2.ErrorCode,pivotMonthTable.pivotErrorCode), isnull (q2.ServiceID, pivotMonthTable.pivotServiceId)

END

GO

```
``

Nothing worked. Maybe the bigint should be without brackets and I should use cast in order to avoid overflow, but I don’t know really what should be done here in the exact situation.

New contributor

Zlatkow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật