I've a table in a (MS) SQL database which has an Id column (identity, int) and a Name column (varchar(250)). However, the values in the name column contain (fairly random) leading and trailing spaces as I think they were cut and pasted from "something else" (no idea what!).
Is it possible in T-SQL to do the following:
update MyTable set Name = trim(name)
and have it update all the Name columns with the trimmed value?
SQL Server TRIM() FunctionThe TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.
Click in the first cell of the Trim column. On the Formulas tab, click the Text dropdown menu in the Functions group and select TRIM.
LTrim() and RTrim() Function in MS AccessIn LTrim() function a string will be pass as a parameter and it will return the string with no leading spaces. 2. RTrim() Function : It works same like LTrim() function but it will remove all trailing spaces from a string.
SQL Server LTRIM() Function The LTRIM() function removes leading spaces from a string.
MS SQL does not have a trim function. You'll need to use rTrim and lTrim together.
update MyTable set Name = lTrim(rTrim(name))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With