m4tm4t
m4tm4t

Reputation: 2381

SQL Server : UPPER last five chars

I have the following working query :

SELECT
    REPLACE(
        AddressLine1, RIGHT( AddressLine1, 5 ),
        UPPER( RIGHT( AddressLine1, 5 ) )
    )
FROM Person.Address

There is other way to do that ?

Upvotes: 2

Views: 63

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269693

Use concatenation:

select left(addressline1, len(addressline1) - 5) + upper(right(addressline1, 5))
from person.address;

Your method is dangerous, because there is no guarantee that the last five characters only appear once in the address.

Hmmm, you can also do:

select stuff(addressline1, len(addressline1) - 4, 5, upper(right(addressline1, 5)))
from person.address;

Both these formulations assume that addressline1 has at least 5 characters. If shorter addresses are possible, then a case is probably the easiest solution.

Upvotes: 1

Related Questions