TSQL Script to remove TAB, LINE FEED and CARRIAGE RETURN

Posted on : 31-08-2012 | By : Devi Prasad | In : T-SQL Code Examples

Share:

0


The below script removes the TAB(Horozontal Tab), Line feed(New line), Carriage Return Characters in a variable @String

SET NOCOUNT ON
DECLARE @String VARCHAR(100)
DECLARE @CorrectedString VARCHAR(100)

SELECT @String = 'AB	C
D'

PRINT @String

SELECT @CorrectedString = REPLACE(@String, CHAR(9),'')
PRINT @CorrectedString

SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(10),'')
PRINT @CorrectedString

SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(13),'')
PRINT @CorrectedString

Extended script which also provides length of the string:

SET NOCOUNT ON
DECLARE @String VARCHAR(100)
DECLARE @CorrectedString VARCHAR(100)

SELECT @String = 'AB	C
D'

PRINT @String
PRINT 'LENGTH='+CAST(LEN(@String) AS VARCHAR(5))

SELECT @CorrectedString = REPLACE(@String, CHAR(9),'')
PRINT @CorrectedString
PRINT 'LENGTH='+CAST(LEN(@CorrectedString) AS VARCHAR(5))

SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(10),'')
PRINT @CorrectedString
PRINT 'LENGTH='+CAST(LEN(@CorrectedString) AS VARCHAR(5))

SELECT @CorrectedString = REPLACE(@CorrectedString, CHAR(13),'')
PRINT @CorrectedString
PRINT 'LENGTH='+CAST(LEN(@CorrectedString) AS VARCHAR(5))
(Visited 2,355 times, 1 visits today)



References : Devi Prasad (sqlserverlearner.com)

Need Help On SQL Server?

Cannot Find Solution to your problem (or) If you are looking for some help on SQL Server. Dont worry Click Here to Post your question and solve your issue.


Do you like my blog?

If you liked reading this blog, please help spread the word by sharing this blog with your friends.




Write a comment