1

I have a field named rspec in a table trace.

So for now the field is like "Vol3/data/20070204_191426_FXBS.v3a".

All I need is a query to change it to the format "Vol3/data/20070204_191426_FXBS.V3A".

1
  • Do you only want to make the "extension" uppercase, or anything behind the second /? Commented Feb 15, 2012 at 23:02

2 Answers 2

2

Assuming the current version:

select left(rspec, - 3)||upper(right(rspec, 3))
from trace

For older versions:

select substr(rspec, 1, length(rspec) - 3)||upper(substring(rspec from '...$'))
from trace
Sign up to request clarification or add additional context in comments.

1 Comment

+ 1 elegant use of the new left() with a negative length.
2

Or, to cover all possibilities like

  • file extensions of variable length: abc123.jpeg
  • no file extension at all: abc123
  • dot as last character: abc123.
  • multiple dots: abc.123.jpg

SELECT CASE WHEN rspec  ~~ '%.%'
          THEN substring(rspec, E'^.*\\.')
            || upper(substring(rspec , E'([^.]*)$'))
          ELSE rspec
       END AS rspec 
FROM  (VALUES
         ('abc123.jpeg')
       , ('abc123')
       , ('abc123.')
       , ('abc.123.jpg')
         )  ASx(rspec); -- testcases

Explain:

If the string has no dot, use the string.
Else, take everything up to and including the last dot in the string.
Append everything after the last dot in upper case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.