Your
@str
variable is a one-character substring. It can never match the condition
@str like '%and%'
, since that would require at least a three-character substring.
Try:
CREATE OR ALTER FUNCTION dbo.translationkey_v13
(
@column_name nchar(15)
)
Returns nvarchar(1000)
As
BEGIN
DECLARE @result nvarchar(1000);
SET @result = @column_name;
SET @result = Replace(@result, N'and', N'_');
SET @result = Replace(@result, N' ', N'_');
WHILE @result Like N'%[_][_]%'
BEGIN
SET @result = Replace(@result, N'__', N'_');
END;
Return N'crop.group.' + LOWER(@result);
END;
NB: This won't work well if the input contains "and" as part of a larger word. For example, given the input "sand and cement", the output would be "s_cement".
If you only want to remove "and" between other words, change the first
Replace
to:
SET @result = Replace(@result, N' and ', N'_');