Solved

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

Posted on 2014-07-18
9
483 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 47

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 47

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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