|
-
Dec 7th, 2010, 12:12 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Trim a carriage return from end of string
I need to alter a select statement so that a returned field does not have an unwanted carriage return at the end.
I think the code below would remove all the carriage returns but I only want to remove one if it appears at the very end of the string.
SELECT REPLACE([parameters],Char(10),'') as [parameters]
FROM QUERIES
-
Dec 7th, 2010, 12:14 PM
#2
Re: Trim a carriage return from end of string
Which database system(s) are you using?
-
Dec 7th, 2010, 12:19 PM
#3
Thread Starter
Frenzied Member
Re: Trim a carriage return from end of string
Sorry it is SQL Server 2010.
-
Dec 7th, 2010, 12:40 PM
#4
Re: Trim a carriage return from end of string
If it is just Char(10) you are interested in, something like this should do it:
Code:
SELECT CASE WHEN Right([parameters],1) = Char(10)
THEN Left([parameters], Length([parameters])-1)
ELSE [parameters]
END as [parameters]
FROM QUERIES
If you want to allow a mixture of Char(10) and Char(13) it gets a bit more awkward, and it would probably be better to create a function.
-
Dec 7th, 2010, 12:49 PM
#5
Re: Trim a carriage return from end of string
I did not know there was a SQL Server 2010. Where did you find that. I thought the last relase was SQL Server 2008R2 and the SQL Server "Denali" CTP1 was the latest preview.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Dec 7th, 2010, 01:18 PM
#6
Re: Trim a carriage return from end of string
(not tested code)
sql Code:
CREATE FUNCTION RTrimChar(@Input VARCHAR(MAX), @TrimChar CHAR(1)) RETURNS VARCHAR(MAX) AS BEGIN WHILE RIGHT(@Input, 1) = @TrimChar SET @Input = LEFT(@Input, LEN(@Input)-1) RETURN @Input END
-
Dec 8th, 2010, 04:11 AM
#7
Thread Starter
Frenzied Member
Re: Trim a carriage return from end of string
 Originally Posted by si_the_geek
If it is just Char(10) you are interested in, something like this should do it:
Code:
SELECT CASE WHEN Right([parameters],1) = Char(10)
THEN Left([parameters], Length([parameters])-1)
ELSE [parameters]
END as [parameters]
FROM QUERIES
If you want to allow a mixture of Char(10) and Char(13) it gets a bit more awkward, and it would probably be better to create a function.
I In actual fact I do want to remove Char13 but I need to remove all Char13 from the string but only remove a Char10 if it is at the end of the string.
REPLACE(CASE WHEN Right([parameters],1) = Char(10)
THEN Left([parameters], Len([parameters])-1)
ELSE [parameters]
END,Char(13),'')
as [parameters]
Currently tried this but it still does not seem to remove the extra carriage return at the end of my string.
If I copy and paste the output to word here is what it returns (hope you can figure out what I I mean by the improvisation of symbols at the end of each row.).
Start Date : 07/12/2010<¬
End Date : 08/12/2010<¬
Include IT : Y<¬
Report Names : Select All P|
P|
Last edited by FishGuy; Dec 8th, 2010 at 04:57 AM.
-
Dec 8th, 2010, 05:09 AM
#8
Re: Trim a carriage return from end of string
Try adding Pradeep1210's function, then using this:
Code:
SELECT RTrimChar (REPLACE([parameters],Char(13),''), Char(10))
...
-
Dec 8th, 2010, 05:21 AM
#9
Thread Starter
Frenzied Member
Re: Trim a carriage return from end of string
I just tried that, is it possible that there are two CRs at the end of the string, how would the function handle that?
Alternatively, is there another char that could be causing this i.e. not char10 or 13?
Last edited by FishGuy; Dec 8th, 2010 at 05:30 AM.
-
Dec 8th, 2010, 05:23 AM
#10
Re: Trim a carriage return from end of string
Just like RTrim function removes all trailing spaces, this function removes all trailing custom character you specify. Doesn't matter how many they are.
-
Dec 8th, 2010, 05:25 AM
#11
Re: Trim a carriage return from end of string
What it wont handle however is any other characters (such as spaces) between those characters, so it may be a good idea to add this:
Code:
SET @Input = RTrim(LEFT(@Input, LEN(@Input)-1))
-
Dec 8th, 2010, 05:54 AM
#12
Thread Starter
Frenzied Member
Re: Trim a carriage return from end of string
Still not working, is there any way to get the actual char output from the data to see what is going there.
-
Dec 8th, 2010, 06:15 AM
#13
Thread Starter
Frenzied Member
Re: Trim a carriage return from end of string
Thanks guys, that now works.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|