0

I'm new in sqlplus, and I'm having difficulties with the basic usage of variables :)

I have the following:

define PROJECT_VERSION='1.1-SNAPSHOT'

I'd like to create an other variable, which contains the same info without the -SNAPSHOT part (so its value is 1.1). Of course I don't want to type define PROJECT_VERSION_WOSNAP='1.1', instead of it something like define PROJECT_VERSION_WOSNAP=replace(&PROJECT_VERSION,'-SNAPSHOT','') were more appropriate.

How can I do it?

Thank You in advance.

1 Answer 1

1

What you suggested almost works; you need to enclose your base variable in single quotes though:

define PROJECT_VERSION_WOSNAP=replace('&PROJECT_VERSION','-SNAPSHOT',null)

select &PROJECT_VERSION_WOSNAP from dual;

old:select &PROJECT_VERSION_WOSNAP from dual
new:select replace('1.1-SNAPSHOT','-SNAPSHOT',null) from dual

REP
---
1.1

I've left verify on so you can see that the replace() operation will be repeated every time that substitution variable is referenced later, which isn't necessarily a big deal, but something to be aware of.

You could also use the column ... new_value syntax to define the variable:

undefine PROJECT_VERSION_WOSNAP
column TMP_VERSION new_value PROJECT_VERSION_WOSNAP
select replace('&PROJECT_VERSION','-SNAPSHOT',null) as TMP_VERSION from dual;

old:select replace('&PROJECT_VERSION','-SNAPSHOT',null) as TMP_VERSION from dual
new:select replace('1.1-SNAPSHOT','-SNAPSHOT',null) as TMP_VERSION from dual

TMP
---
1.1

select &PROJECT_VERSION_WOSNAP from dual;

old:select &PROJECT_VERSION_WOSNAP from dual
new:select 1.1 from dual

       1.1
----------
       1.1

Now you can see that references use the value directly, without repeating the replace() - it only happens once in that column-setting query. You can set termout off and then on around the query that sets the variable to hide it, in addition to set verify off.

Another approach is to define the variables the other way around:

undefine PROJECT_VERSION
undefine PROJECT_VERSION_WOSNAP
define PROJECT_VERSION_WOSNAP=1.1
define PROJECT_VERSION='&PROJECT_VERSION_WOSNAP.-SNAPHOT'

select '&PROJECT_VERSION', &PROJECT_VERSION_WOSNAP from dual;

old:select '&PROJECT_VERSION', &PROJECT_VERSION_WOSNAP from dual
new:select '1.1-SNAPHOT', 1.1 from dual

'1.1-SNAPHO        1.1
----------- ----------
1.1-SNAPHOT        1.1

but that assumes the snapshot part if always required, and from the way you asked the question I suspect that can change.

Yet another approach is to use bind variables throughout instead of substitution variables:

var PROJECT_VERSION varchar2(20);
var PROJECT_VERSION_WOSNAP varchar2(20);
exec :PROJECT_VERSION := '1.1-SNAPSHOT';
exec :PROJECT_VERSION_WOSNAP := replace(:PROJECT_VERSION,'-SNAPSHOT',null);

select :PROJECT_VERSION, :PROJECT_VERSION_WOSNAP from dual;

:PROJECT_VERSION                 :PROJECT_VERSION_WOSNAP
-------------------------------- --------------------------------
1.1-SNAPSHOT                     1.1

but again that might not work for whatever you are doing later.

Sign up to request clarification or add additional context in comments.

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.