Solved

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

Posted on 2014-07-18
9
454 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
  • 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
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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I subtract date and time within a same column in SQL 4 38
string fuctions 4 26
Text file into sql server 5 27
Better way to make a query with date filter. 5 27
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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