Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete leading empty space in a SQL Database Table using MS SQL Server Management Studio

Tags:

sql

sql-server

I imported an Excel sheet with countries into a database table.

Unfortunately all rows have some leading empty space.

So, how can I delete these empty spaces?

like image 326
andalusi Avatar asked Jan 24 '13 12:01

andalusi


People also ask

How do I remove leading zeros in SQL Server?

Basically it performs three steps: Replace each 0 with a space – REPLACE([CustomerKey], '0', ' ') Use the LTRIM string function to trim leading spaces – LTRIM(<Step #1 Result>) Lastly, replace all spaces back to 0 – REPLACE(<Step #2 Result>, ' ', '0')

How do I free up unused spaces in SQL Server?

Export all the rows to a new table and move the rows back. This reorganizes the LOB data and release the unused space. Use DBCC SHRINKFILE with EMPTYFILE option to move all the data to a newly added data file and then remove old data file. This reorganizes the LOB data there by releasing the unused space.


1 Answers

This will remove leading and trailing spaces

Update tablename set fieldName = ltrim(rtrim(fieldName));

some versions of SQL Support

Update tablename set fieldName = trim(fieldName);

If you just want to remove leading

update tablename set fieldName = LTRIM(fieldName);
like image 126
xQbert Avatar answered Sep 29 '22 12:09

xQbert