SQL code to ommit for Char(13)


(select *,
from (
select *, lb5=nullif(charindex(char(10),n,lb4+1),0) from (
select *, lb4=nullif(charindex(char(10),n,lb3+1),0) from (
select *, lb3=nullif(charindex(char(10),n,lb2+1),0)from (
select *, lb2=nullif(charindex(char(10),n,lb1+1),0) from (
select *, lb1=nullif(charindex(char(10),n,0),0) from (
select *, n=DeliveryStreet+CHAR(10)
from MySalesTable
)[0])[1])[2])[3])[4])[5]) ST

I have this piece of Code that works ok if no carriage return(Char(13)) put in on the last line of the address.
If there is  a carriage return then I get the following error

Msg 536, Level 16, State 5, Line 1
Invalid length parameter passed to the SUBSTRING function.

How can I amend the above code  to not crash when a char(13) encountered ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
In general you can always use T-SQL REPLACE function and char() to remove cr's
Declare @str varchar(100) = 'yabba
and tigger too'

SELECT REPLACE(@str, CHAR(13), ' ') 

Open in new window

@Jim... I'm a bit concerned with REPLACE stripping out the CR and LF in the middle of the string.

put in on the last line of the address
This will remove the last CHAR(10) or CHAR(13). Makes the assumption that the last valid character on the address is an alphanumeric character.
declare @address varchar(255) = 'line 1
line 2
line 3

print '-----------------------------'
print 'BEFORE: ''' + @address + ''''
print '-----------------------------'

select @address = left(@address, len(@address) - patindex('%[A-Z0-9.]%', reverse(@address)) + 1)

print 'AFTER: ''' + @address + ''''
print '-----------------------------'

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Some sample data with an expected result set would help.
MECR123Author Commented:

I have an address in a table - it is held as Varchar(250)

the addresses are shown on the screens as

The code above will work fine if the user inputting the address does not press return after the last line of the address - if they do then my code fails with the error above.  Is there a way in my Select statement above to ignore this last return carriage character if it was input by the user.
Please try out the example I gave you. It does just that.

I'm just a but unsure how to integrate into your query.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.