I have a script which is accepting "Black AND White" but Idf do like "Black & White" its returning like "Black White" . How to make it work for bot AND and & in sql server. below is the script.
DECLARE @Title varchar(255)='Black AND White'
DECLARE @RawDescription varchar(255) =@Title
WHILE PATINDEX('%[^ ''0-9A-Za-z]%', @RawDescription) > 0
BEGIN
IF SUBSTRING(@RawDescription, PATINDEX('%[^ ''0-9A-Za-z]%', @RawDescription), 1) = '-'
BEGIN
SET @RawDescription = TRIM(REPLACE(@RawDescription, SUBSTRING(@RawDescription, PATINDEX('%[^ ''0-9A-Za-z]%', @RawDescription), 1), ' '))
END
ELSE
BEGIN
SET @RawDescription = TRIM(REPLACE(@RawDescription, SUBSTRING(@RawDescription, PATINDEX('%[^ ''0-9A-Za-z]%', @RawDescription), 1), ''))
END
END
WHILE CHARINDEX(' ', @RawDescription) > 0
BEGIN
SET @RawDescription = REPLACE(@RawDescription, ' ', ' ')
END
select LTRIM(RTRIM(@RawDescription))
Jayraj ChhayaPosted Nov 20, 2024, 5:44 AM
Hi Pinku,
To achieve the desired functionality where both "AND" and "&" are treated the same in your SQL script, you can modify the existing script to replace "&" with "AND" before processing the string. This way, both terms will be handled uniformly. Below is the revised version of your script:
In this updated script, the line
DECLARE @RawDescription varchar(255) = REPLACE(@Title, '&', 'AND')ensures that any occurrence of "&" in the input string is replaced with "AND" before further processing. This allows your script to handle both terms seamlessly.Jignesh KumarPosted Nov 20, 2024, 5:34 AM
Hello Rinku,
Please this script which will work for you,
Sangeetha SPosted Nov 20, 2024, 5:08 AM
Hi,
SQL script accept both "AND" and "&" as separators in string
Replace '&' with 'AND':