Please help me to remove the first and last character if it has specific character (#
) in the string otherwise do not remove?
Input 1: #Test#SQL#
Desired Output : Test#SQL
Input 2: Test#SQL
Desired Output : Test#SQL
Thank you in advance
Just an another perspective by using a CASE
expression and a combination of LEFT
and RIGHT
string functions.
Query
select case when left(@str, 1) = '#' and right(@str, 1) = '#'
then left((right(@str, len(@str) - 1)), len((right(@str, len(@str) - 1))) - 1)
when left(@str, 1) = '#' and right(@str, 1) <> '#'
then right(@str, len(@str) - 1)
when left(@str, 1) <> '#' and right(@str, 1) = '#'
then left(@str, len(@str) - 1)
else @str end as str;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments