String manipulation : Need to change the first and last letters to upper case

  • I need to change the first and last letter to upper case . The below piece of the code works , just wanted to know if there is a better way of doing it .

    declare @name varchar(100)

    Select @name = 'test'

    print Upper(substring(@name,1,1))+Substring(@name,2,Len(@name)-2) + Upper(substring(@name,(len(@name)-0),Len(@name)))

  • 3 possible options. I used LOWER, but I'm not sure if you need it.

    PRINT UPPER(LEFT(@name,1)) + LOWER( SUBSTRING(@name,2,Len(@name)-2)) + UPPER(right(@name,1))

    PRINT STUFF( STUFF( LOWER(@Name), 1, 1, UPPER(LEFT(@name,1))), LEN(@name), 1, UPPER(RIGHT(@name,1)))

    PRINT UPPER(LEFT(@name,1)) + STUFF( LOWER(SUBSTRING(@name,2, 100)), LEN(@name) - 1, 1, UPPER(RIGHT(@name,1)))

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • You've said letter.

    What about strings like Select @name = 'test2' or Select @name = ' test' , what result is expected?

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply