Answered by:
Leading and Trailing Spaces

Question
-
I am having a issue with Leading and Trailing Spaces. I have values that are being showed duplicates because they have leading, Trailing or any unoticiable ascii characters. I want to ensure that i remove them so the duplicates values can be handled. I tried using in Query but i still had no success.
thoguhts ?
SELECT Ltrim(Rtrim(FTA.BeforeValue)) BeforeValue, LEN(Ltrim(Rtrim(FTA.BeforeValue))) FROM Fact_TicketAudit AS FTA INNER JOIN Dim_Auditfield AS AF ON FTA.AuditFieldID = AF.AuditFieldID Where (AF.AuditFieldID = (10002)) Group by Ltrim(Rtrim(FTA.BeforeValue)), LEN(Ltrim(Rtrim(FTA.BeforeValue))) Order by Ltrim(Rtrim(FTA.BeforeValue))
Wednesday, October 17, 2012 7:02 PM
Answers
-
Nested REPLACE is a fast way of getting rid of unwanted characters:
http://www.sqlusa.com/bestpractices2008/nestedreplace/
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Iric Wen Thursday, October 25, 2012 8:59 AM
Wednesday, October 17, 2012 7:29 PM -
this might help:
http://www.tainyan.com/codesnippets/entry-63/sql-function-to-remove-special-characters.html
can you please provide sample data.
Wednesday, October 17, 2012 7:05 PM -
Oops, somehow I closed the window before posting.
I have two blogs on a similar topic
and
http://beyondrelational.com/modules/2/blogs/78/posts/11141/remove-bad-characters-from-a-string.aspx
For every expert, there is an equal and opposite expert. - Becker's Law
My blogWednesday, October 17, 2012 8:15 PM
All replies
-
this might help:
http://www.tainyan.com/codesnippets/entry-63/sql-function-to-remove-special-characters.html
can you please provide sample data.
Wednesday, October 17, 2012 7:05 PM -
try :
CREATE FUNCTION [dbo].[ufn_RemoveSpecialChars]
(
@Input VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
BEGIN
DECLARE @Output VARCHAR(MAX )
IF (ISNULL(@Input,'')='')
SET @Output = @Input
ELSE
BEGIN
DECLARE @Len INT
DECLARE @Counter INT
DECLARE @CharCode INT
SET @Output = ''
SET @Len = LEN(@Input)
SET @Counter = 1
WHILE @Counter <= @Len
BEGIN
SET @CharCode = ASCII(SUBSTRING(@Input, @Counter, 1))
IF @CharCode=32 OR @CharCode BETWEEN 48 and 57 OR @CharCode BETWEEN 65 AND 90 OR @CharCode BETWEEN 97 AND 122
SET @Output = @Output + CHAR(@CharCode)
SET @Counter = @Counter + 1
END
END
RETURN Ltrim(rtrim(@Output))
END
---
SELECT dbo.[ufn_RemoveSpecialChars](' This^ is $- sample ~text.#&*' )
Wednesday, October 17, 2012 7:20 PM -
Nested REPLACE is a fast way of getting rid of unwanted characters:
http://www.sqlusa.com/bestpractices2008/nestedreplace/
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Iric Wen Thursday, October 25, 2012 8:59 AM
Wednesday, October 17, 2012 7:29 PM -
Oops, somehow I closed the window before posting.
I have two blogs on a similar topic
and
http://beyondrelational.com/modules/2/blogs/78/posts/11141/remove-bad-characters-from-a-string.aspx
For every expert, there is an equal and opposite expert. - Becker's Law
My blogWednesday, October 17, 2012 8:15 PM -
Are your columns CHAR, by any chance instead of VarChar? (This would explain Ltrim/Rtrim not behaving as expected with spaces)Wednesday, October 17, 2012 8:50 PM
-
I tried SEVERAL times to click 'Propose As Answer' for Naomi's answer. I get an error message every time. I tried in 2 browsers; I logged out and logged in again; I just keep getting error messages here. Oh well. That would be my proposed answer.
Great job, now and always, Naomi!!
Ryan Shuell
Saturday, October 20, 2012 2:24 AM -
I tried SEVERAL times to click 'Propose As Answer' for Naomi's answer. I get an error message every time. I tried in 2 browsers; I logged out and logged in again; I just keep getting error messages here. Oh well. That would be my proposed answer.
Great job, now and always, Naomi!!
Ryan Shuell
Thanks for reporting.Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012Saturday, October 20, 2012 3:43 AM