0
CREATE PROCEDURE [dbo].[namecheck]
    (@surname varchar(40))
AS
if @surname like '%[^0-9a-zA-Z .,?()\[\]:;''=/@$%*&!"-]%' ESCAPE '\' COLLATE Latin1_General_100_CS_AS
    RETURN 1

RETURN 2
GO

DECLARE @returncode int

EXECUTE @returncode = namecheck 'Bum'

SELECT @returncode AS returncode

EXECUTE @returncode = namecheck 'Bumë' 

SELECT @returncode AS returncode

GO

the return code is the same regardless of the validity of the string eg 'Bum' should be returncode 2 and 'Bumë' should be return code 1

Warlock
  • 1
  • 1
  • 1
    can you please remove the commented code ? It makes the code little more difficult to go through. – Venkataraman R Feb 23 '22 at 03:25
  • if your requirement is to remove NON-ASCII characters, you can refer to post: https://stackoverflow.com/questions/3891331/find-non-ascii-characters-in-varchar-columns-using-sql-server – Venkataraman R Feb 23 '22 at 03:51

2 Answers2

0

You should check for the presence of any non whitelisted characters:

if @surname like '%[^0-9a-zA-Z .,?()\[\]:;''=/@$%*&!"-]%' escape '\' collate Latin1_General_100_CS_AS
    begin
    select @message  = 'The surname name contains invalid characters'
    return 13
    end
else
    begin
    select @message  = 'The surname name contains valid characters'
    return 12
    end
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Something like :

CREATE OR ALTER FUNCTION dbo.F_VALID_STRING (@STRING NVARCHAR(max))
RETURNS BIT
AS
BEGIN
IF @STRING NOT LIKE REPLICATE('[a-zA-Z0-9]', LEN (@STRING)) COLLATE Latin1_General_BIN
   RETURN 0;
DECLARE @STR NVARCHAR(max) = TRANSLATE(@STRING, ' \.,?()[]:;''-=/@$%*!"', '&&&&&&&&&&&&&&&&&&&&&');
SET @STR = REPLACE(@STR, '&', '');
IF LEN(@STR) <> LEN(@STRING)
   RETURN 0;
RETURN 1
END
GO

May help you...

SQLpro
  • 3,994
  • 1
  • 6
  • 14