For Example; SELECT TRIM(Names) FROM Customer
this is the easiest way to do it, just remember to alias your column being returned
Miles
2008-10-08 15:10:37
@Miles - I added the alias for completeness.
Ben Hoffstein
2008-10-08 15:29:43
+17
A:
To Trim on the right, use:
SELECT RTRIM(Names) FROM Customer
To Trim on the left, use:
SELECT LTRIM(Names) FROM Customer
To Trim on the both sides, use:
SELECT LTRIM(RTRIM(Names)) FROM Customer
Kibbee
2008-10-07 17:56:38
Incidentally, what possible reason could Microsoft have for including an LTRIM and RTRIM function without a TRIM? It's peculiar.
Ben Hoffstein
2008-10-07 18:06:49
Because it is redundant. You can accomplish the same thing with LTRIM(RTRIM(var)).
Kibbee
2008-10-07 23:11:42
Yes, but that's two function calls. You could say they are all redundant since TSQL has CHARINDEX and SUBSTRING, but that's an idiotic way to look at it.
Ben Hoffstein
2008-10-08 14:02:03
A:
I assume this is a one-off data scrubbing exercise. Once done, ensure you add database constraints to prevent bad data in the future e.g.
ALTER TABLE Customer ADD
CONSTRAINT customer_names__whitespace
CHECK (
Names NOT LIKE ' %'
AND Names NOT LIKE '% '
AND Names NOT LIKE '% %'
);
Also consider disallowing other characters (tab, carriage return, line feed, etc) that may cause problems.
It may also be a good time to split those Names into family_name
, first_name
, etc :)
onedaywhen
2008-10-08 10:25:42