I have a table in sql that has a column named [message]. I need to use a UDF to populate a 1 or a 0 dependant on if that substring contains 'LHL'.
My code:
create function dbo.generate
(
@result varchar(max)
)
returns int
as
begin
declare @message varchar(max) = (Select [message] from POST)
if (PATINDEX('%LHL%', @message) > 0)
begin
set @result = '1'
end
else
set @result = '0'
return @result
end
go
select dbo.generate('%LHL%')
With this I get the error:
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I just need a list that has 0's and 1's. I can generate a list using PATINDEX that can give me where the LHL begins, but cannot seem to change the number to a 1 (if exists) as shown. (also must use a UDF not a CTE)
with cte as
(select [message] from POSt
),
pos as
(select patindex('%LHL%',[message]) pos, [message] from cte
union all
select pos+patindex('%LHL%',substring([message], pos+1, len([message]))) pos, [message] from pos
where patindex('%LHL%',substring([message], pos+1, len([message])))>0
)
select pos from pos