Solved

How to remove char(13) + char(10)  from the END of a string

Posted on 2014-07-18
9
513 Views
Last Modified: 2014-07-21
I have a sql variable that holds appended record set values that include char(13) + char(10) .

At the very end of the string (not within the string) i will need to remove the char(13) + char(10) .

For example i could have a string variable with the following:

Address: char(13) + char(10) 689 Will Street char(13) + char(10) Columbus char(13) + char(10) GA

But the actual result i would be looking to be removed is bolded below:
Address: char(13) + char(10) 689 Will Street char(13) + char(10) Columbus char(13) + char(10) GA


Thanks,
0
Comment
Question by:MoreThanDoubled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 40205600
Didn't we just build this string the other day?

That isn't actually "at the very end of the string" is it, since the state characters follow those values.

You might start by using the InstrRev( ) function to look for the last position where those to characters are found together in the sting.

To check your table, you could try:

SELECT [yourField], Left(yourField, instrrev([yourField], chr$(13) & chr$(10))-1)
FROM yourTable
WHERE InstrRev(yourString, chr$(13) & chr$(10)) <> 0
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40205672
Do you just want to SELECT the rows or do you want to UPDATE the values?

Here's the SELECT to do that; if you press Ctrl-T before running, you can see the affects of the CR+LF in the output results:

SELECT
    address_string AS address_string,
    STUFF(address_string, LEN(address_string) - CHARINDEX(char(10) + char(13), REVERSE(address_string)), 2, ' ') AS address_string_new

FROM (
    select char(13) + char(10) + '689 Will Street' + char(13) + char(10) + 'Columbus' + char(13) + char(10) + 'GA' as address_string
) AS test_data
0
 
LVL 32

Expert Comment

by:awking00
ID: 40206374
with t as
(select reverse(address) as address,charindex(char(10) + char(13), reverse(address) - 1) as pos
 from yourtable)
select reverse(left(address,pos) + ' ' + right(address,len(address) - pos - 2)) as newaddress
from t;
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 48

Expert Comment

by:Dale Fye (Access MVP)
ID: 40206435
Sorry, i missed that this was posted to the SQL Server topic area
0
 
LVL 30

Accepted Solution

by:
hnasr earned 500 total points
ID: 40207125
For the mentioned  pattern, compare with:

declare @p varchar(100)
set @p='char(13) + char(10) 689 Will Street char(13) + char(10) Columbus char(13) + char(10) GA';
select left(@p,len(@p)-23)+right(@p,3)

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 40208882
How well does that work if the address line was? -
Address: + char(13) + char(10) +  1 Main St. + char(13) + char(10) +  Ames + char(13) + char(10) +  IA
0
 
LVL 30

Expert Comment

by:hnasr
ID: 40209053
awking00,

For the mentioned  pattern, compare with:
set @p='+ char(13) + char(10) +  1 Main St. + char(13) + char(10) +  Ames + char(13) + char(10) +  IA '
set @p= rtrim(replace(@p,'  ',' '));
select left(@p,len(@p)-26)+right(@p,3)

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 40209109
hnasr,
I agree that what you have posted will return what is wanted in these two cases, but I think what is really desired is code that will work for any combination of addresses.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40209132
Apparently that's not what is really desired, since I posted such code two days before hnasr's code.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question