Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with MS SQL Right Function Using the Len Function

Posted on 2014-07-23
5
Medium Priority
?
477 Views
Last Modified: 2014-07-23
Hi

I have a column in a table which is made up of reference numbers following a transaction matching process.  It's there to form part of an audit trail.  At the moment, it might look something like ", 88654, 12973, 84755".  It might also be empty (if no transactions matched).  I want to get rid of the comma and space at the beginning.  I don't really want to do this as part of the original run as it would slow it down, so thought it would be easiest to do a tidy up afterwards.

The problem is that I can't get the right function to work if I use the len function.  I've tried various, but at the moment, I've got:

UPDATE SAS_T_1213_RemAds SET remRef = RIGHT(conCatRem,LEN(conCatRem)-2) WHERE conCatRem IS NOT NULL;

I get the error message:

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

Can anyone help with this please?

Thanks
Sarah
0
Comment
Question by:ScuzzyJo
[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
5 Comments
 
LVL 21

Accepted Solution

by:
Randy Poole earned 2000 total points
ID: 40214208
try this, the value may not be null, but may be a blank or under 2 characters
UPDATE SAS_T_1213_RemAds SET remRef = RIGHT(conCatRem,LEN(conCatRem)-2) WHERE Len(conCatRem+'')>2

Open in new window

0
 
LVL 25

Expert Comment

by:SStory
ID: 40214213
Try something like this:
UPDATE SAS_T_1213_RemAds 
SET remRef = SUBSTRING(remRef,3,len(remRef)-2) 
WHERE conCatRem IS NOT NULL;

Open in new window

0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40214214
Hi,

You need to check the length of conCatRem before passing it to the Right function so that if the Lengh is less then 2 Right function would not get any -Ve Value which is causing error

UPDATE SAS_T_1213_RemAds SET remRef = CASE WHEN LEN(conCatRem) >= 2 THNE (RIGHT(conCatRem,LEN(conCatRem)-2) ELSE conCatRem END  WHERE conCatRem IS NOT NULL;

Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40214224
what is the defined length of that column? let's say it is 4000

test it first, always backup...
select
SUBSTRING(remRef ,3, 4000)
from SAS_T_1213_RemAds 
where remRef like ' ,%'
;

UPDATE SAS_T_1213_RemAds 
SET remRef = SUBSTRING(remRef ,3, 4000)
where remRef like ' ,%'

Open in new window

0
 

Author Closing Comment

by:ScuzzyJo
ID: 40214301
Thanks to everyone who replied.  All the answers made sense to me, but Randy got there first and his solution works perfectly :-)
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

704 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