0
votes

I Have Temp table which contains the data which is imported from csv file, i want to remove the white space from the empty columns. tried with RTRIM(LTRIM(column)), it is just removing the leading and trailing spaces if string is not null.

2
Please, post some sample data and desired result.McNets
Well LTRIM removes spaces on the left and RTRIM removes trailing spaces. If you want to remove ALL space you need to use REPLACE.Sean Lange

2 Answers

2
votes

I think the easiest way is to use REPLACE:

SELECT REPLACE(' Test done ', ' ', '')

Or cascade of REPLACE's:

DECLARE @text nvarchar(MAX)=N' Line test'+CHAR(13)+CHAR(10)+'done ';
SELECT REPLACE(REPLACE(REPLACE(@text, ' ', ''), CHAR(13), ''), CHAR(10), '')

The latter removes space, line feed and carriage return characters. Just expand expression to remove more white characters (i.e. tab).

0
votes

I'm gonna go out on a lim here and assume you want to change the values imported from the csv to NULL if they only contain white spaces or empty strings.

If I'm right, this should do the trick:

SELECT NULLIF(LTRIM(RTRIM(ColumnName)), '') As ColumnName