11 December 2013

LTRIM and RTRIM Functions

                Notice that RIGHT and LEFT functions don't check for blank characters. In other words if your string contains a couple of leading blanks then LEFT(string_variable, 2) will return you two blank spaces, which might not be exactly what you want. If your data needs to be left aligned you can use LTRIM function, which removes the leading blanks. Similarly the RTRIM function removes the trailing characters. For instance, the following UPDATE statement will left align (remove any number of leading blanks) last names:  

UPDATE DimEmployee SET LastName = LTRIM(LastName) 

 Similarly, if your data is padded with spaces and you don't wish to see spaces in your output you can use the RTRIM function. You could combine the two functions to remove both leading and trailing spaces as follows:  

UPDATE DimEmployee SET LastName = LTRIM(RTRIM(LastName)

No comments:

Post a Comment