0 votes
1 view
in SQL by (2.3k points)

I'm in the process of trying to clear out leading and trailing spaces from an NVARCHAR(MAX) column that is filled with prices (using NVARCHAR due to data importing from multiple operating systems with odd characters).

At this point, I have an at-SQL command that can remove the leading/trailing spaces from static prices. However, when it comes to leveraging this same command to remove all prices, I'm stumped.

Here's the static script I used to remove a specific price:

UPDATE *tablename* set *columnname* = LTRIM(RTRIM(2.50)) WHERE cost = '2.50 ';

1 Answer

0 votes
by (4.3k points)

To use LTRIM/RTRIM to search and replace leading/trailing spaces in SQL you need to follow this step...

To remove spaces from left we use LTRIM

To remove spaces from right we use RTRIM

 

UPDATE *tablename* 

SET *columnname* = LTRIM(RTRIM(*columnname*));

would have worked on ALL the rows. To minimize updates if you don't need to update, the update code is unchanged, but the LIKE expression in the WHERE clause would have been

UPDATE [tablename] SET [columnname] = LTRIM(RTRIM([columnname])) WHERE 32 in (ASCII([columname]), ASCII(REVERSE([columname])));

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 14, 2019 in Python by Sammy (47.8k points)
+1 vote
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...