Hi,
I've got this SQL function
USE [TEGS2]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[udfHazPropsList](@ConsignmentNote varchar(255),@HazCode varchar(255))
RETURNS varchar(max)
AS
BEGIN
DECLARE @ResultVar varchar(max)
DECLARE @HazProps varchar(max)
--get a string of all the codes compined
select @HazProps=stuff( (select ',' + ltrim(rtrim( hazprops ))
from vwWasteReturn WHERE consignmentNotenumber=@ConsignmentNote AND ewc_code=@HazCode
for xml path('')),1,1,'');
--now split the screen, removing duplicates and sorting
SET @HazProps =REPLACE(@HazProps,'HP','')
;with cte as
(
select distinct(Item) from dbo.delimitedSplit8K(@HazProps,',')
)
, cte2 as
(
select cast(item as integer) as x from cte where ISNUMERIC(item)=1
)
select @ResultVar=stuff( (select ', ' + 'HP'+ cast(x as varchar)
from cte2
order by x
for xml path('')),1,1,'');
REturn @resultvar
END
It has been working fine until this afternoon when it now returns the message - [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting the varchar value '14' to data type int - the data in the table hasn't changed and I can't work out why it is now doing this?
The code has to combine strings of codes e.g HP14, HP2, HP1 and HP1, HP3, HP5 in a different record would become HP1, HP2, HP3, HP5 and HP14.
Any help would be much appreciated.
Thanks
What I have tried:
Looking for typos in records which has caused this issue in the past e.g. using . instead of , between the different codes