Tuesday, February 23, 2010

Using SQL Remove the Last character in a String

There are cases when we may need to remove only the last character after we have done some concatenation.
In SQL Query we can do it using Substring and Left


SUBSTRING (Text,Start_From,Till_lenght)

Remove a comma, at the end of the string

Eg: select substring('1,2,3,4,5,', 1, (len('1,2,3,4,5,') - 1))

LEFT gives and output of the string from starting to end

Eg: SELECT LEFT(String, LEN(String) - 1)

The same can be used to trim data from the Right
Eg: SELECT RIGHT(String, LEN(String) - 1)