Remove first and last character if has specific character in SQL Server?

ANR

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

Ullas

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;

Find a demo here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace first or last character is * with % SQL server

From Dev

SQL Remove Last Character if it is "-"

From Dev

Remove first and last character if present

From Dev

How to remove all except the first 3 and last of a specific character with sed

From Dev

How to remove a specific character from a string, only when it is the first or last character in the string.

From Dev

How to remove a specific character from a string, only when it is the first or last character in the string.

From Dev

How to replace first and last character of column in sql server?

From Java

How to remove the first and the last character of a string

From Dev

Remove First and Last Character C++

From Dev

How to remove first and last character of a string in Rust?

From Java

Remove last specific character in a string c#

From Dev

Remove last char if it's a specific character

From Dev

remove the characters after the last of a specific character

From Dev

remove character on the last line that specific word appears

From Dev

Remove spaces in front of any first character and after any last character

From Dev

Remove last character from string in sql plus

From Dev

First and Last Character of a String

From Dev

Replace Last character in SQL Server 2008

From Java

Remove last character of a StringBuilder?

From Dev

Append a specific character after each character of a string in sql server

From Dev

Weird spacing between spans if first span has last character space

From Dev

remove first character in string

From Dev

Replacing a specific Unicode Character in MS SQL Server

From Dev

Finding a Specific Character and/or String SQL Server 2008

From Dev

SQL Server : select all after specific character

From Dev

Remove first and last character at the same time using regex in R

From Dev

Using sed how to remove last character only in the first line

From Dev

Regex: remove all except first character and last number

From Dev

Remove whitespace from first and last character on multiple headline sizes

Related Related

  1. 1

    Replace first or last character is * with % SQL server

  2. 2

    SQL Remove Last Character if it is "-"

  3. 3

    Remove first and last character if present

  4. 4

    How to remove all except the first 3 and last of a specific character with sed

  5. 5

    How to remove a specific character from a string, only when it is the first or last character in the string.

  6. 6

    How to remove a specific character from a string, only when it is the first or last character in the string.

  7. 7

    How to replace first and last character of column in sql server?

  8. 8

    How to remove the first and the last character of a string

  9. 9

    Remove First and Last Character C++

  10. 10

    How to remove first and last character of a string in Rust?

  11. 11

    Remove last specific character in a string c#

  12. 12

    Remove last char if it's a specific character

  13. 13

    remove the characters after the last of a specific character

  14. 14

    remove character on the last line that specific word appears

  15. 15

    Remove spaces in front of any first character and after any last character

  16. 16

    Remove last character from string in sql plus

  17. 17

    First and Last Character of a String

  18. 18

    Replace Last character in SQL Server 2008

  19. 19

    Remove last character of a StringBuilder?

  20. 20

    Append a specific character after each character of a string in sql server

  21. 21

    Weird spacing between spans if first span has last character space

  22. 22

    remove first character in string

  23. 23

    Replacing a specific Unicode Character in MS SQL Server

  24. 24

    Finding a Specific Character and/or String SQL Server 2008

  25. 25

    SQL Server : select all after specific character

  26. 26

    Remove first and last character at the same time using regex in R

  27. 27

    Using sed how to remove last character only in the first line

  28. 28

    Regex: remove all except first character and last number

  29. 29

    Remove whitespace from first and last character on multiple headline sizes

HotTag

Archive