Function To trim Leading and trailing characters from string in sql server


This is Scalar Function To trim Leading and trailing characters from string in SQL Server. As there is no in built function in sql server for the same. Using this one can trim leading/trailing any character(s) from string .

As there is no In-built function in sql server to trim leading characters or trailing characters or both from a string in SQL server. This scalar function will help in doing the same.It takes two parameters:

  • Input string
  • Character to trim
Code-example:Following code-snippet defined it.
Select dbo.fn_RemoveLeadingTrailingChar(',1,2,3,4,5,,,',',')
Output will be:
1,2,3,4,5
It will trim n number of occurrences of the characters that you want to trim in beginning or end of the string.It is well tested . Passing the null or empty string does not throw any error.


Attachments

  • TrimleadingTrailingChar (46449-1-TrimleadingTrailingChar.sql.txt)
  • Related Articles

    How to get rows into comma separated values column in sql

    How to get rows into comma separated values column in sql. Fetching rows values into comma separated column value is required lots of time in our projects. So I tried to achieve this and lastly found one solution. Use this to get all row values into one single column value with comma separation.

    Split Comma separated values into Columns

    In this article, I explain about how to split a string with delimiter into list of rows. De limiter can be comma or plus our requirement is to convert a single row of a table into multiple rows based on number of delimiter.

    More articles: Comma separated value

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: