Stefan Bossbaly
Stefan Bossbaly

Reputation: 6794

Using the LEFT Function with the LEN Function

I am trying to write a function the will cut off the first 4 chars in a field. For example if the value of the field is ABC_123_EFG it would return 123_EFG. I have tried to use a combination of the LEFT and LEN functions but haven't had any success.

Here is what I think it should be ...

RIGHT(code, LEN(code) - 4) AS code_concat

But it fails with this error

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

What am I doing wrong? Is this the best way to accomplish this?

Upvotes: 2

Views: 16219

Answers (3)

Mikael Eriksson
Mikael Eriksson

Reputation: 138960

stuff is nice little function that is quite useful when modifying strings.

select stuff(code, 1, 4, '')

Upvotes: 1

Eric
Eric

Reputation: 95133

Use substring:

 substring(code, 5, 1000)

Or, if it's a varchar(max):

 substring(code, 5, len(code))

Upvotes: 6

TonyP
TonyP

Reputation: 5873

try this..

select isnull(RIGHT(code, LEN(code) - 4),'') AS code_concat

Upvotes: 3

Related Questions