我正在编写一个Oracle函数,我试图将varchar(4)值增加,就像它是一个数字一样,并且如果它最大化则将其翻转回来开始。 因此,如果前一个值为0001,我将其设置为0002.如果它的9999设置为0001。
SELECT the_field INTO some_variable
FROM my_table
WHERE ID = <id I use>;
IF (some_variable= '9999')
some_variable:= '0001';
ELSE
--Not sure here--
END IF;
我在ELSE语句中做什么来增加?
答案 0 :(得分:3)
这似乎是一件奇怪的事情,尤其是因为它似乎回复了一次修改数据的人 - 如果你在某个时刻更新了该ID的行,你可以选择更新来锁定我猜想。
但是假设您想要坚持使用此模型,您可以使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="start">Click To Start</a>
<a href="#" id="stop">Click To Stop</a>
<br>
进行翻转:
mod()
但是,在9999之后,它会将0000作为下一个值。如果您想跳过该值并转到0001,则可以添加select to_char(mod(to_number(the_field, '9999') + 1, 10000), 'FM0000')
from my_table
where id = some_id;
电话:
greatest()
包含一些示例数据的快速演示:
select to_char(greatest(mod(to_number(the_field, '9999') + 1, 10000), 1), 'FM0000')
from my_table
where id = some_id;
答案 1 :(得分:1)
只是要添加上面的答案,你需要输入一个LPAD函数来将前导零添加到增加的数字。
DDL和DML:
create table my_table
(
ID varchar2(4)
,the_field varchar2(100)
);
insert into my_table (ID, the_field) values(1, '0001');
insert into my_table (ID, the_field) values(2, '0002');
insert into my_table (ID, the_field) values(3, '9998');
insert into my_table (ID, the_field) values(4, '9999');
insert into my_table (ID, the_field) values(5, '0014');
insert into my_table (ID, the_field) values(6, '0903');
insert into my_table (ID, the_field) values(7, '2108');
commit;
来自my_table的数据:
--- ----------
ID THE_FIELD
--- ----------
1 0001
2 0002
3 9998
4 9999
5 0014
6 0903
7 2108
PL / SQL块: (P.S.我将SELECT..INTO语句移动到Cursor,所以我可以循环这个例子的所有数据)
SET SERVEROUTPUT ON;
declare
new_some_variable varchar2(100);
Cursor c1 is
SELECT id,
the_field some_variable
FROM my_table;
begin
for c_1 in c1 loop
DBMS_OUTPUT.PUT_LINE('The ID is: '||c_1.ID);
DBMS_OUTPUT.PUT_LINE('Value of Some_Variable before Update: '||c_1.some_variable);
IF (c_1.some_variable = '9999') then
new_some_variable := '0001';
ELSE
new_some_variable := LPAD(TO_NUMBER(c_1.some_variable)+1, 4, '000');
END IF;
DBMS_OUTPUT.PUT_LINE('New Value of Some_Variable After Update: '||new_some_variable);
end loop;
END;
输出:
The ID is: 1
Value of Some_Variable before Update: 0001
New Value of Some_Variable After Update: 0002
The ID is: 2
Value of Some_Variable before Update: 0002
New Value of Some_Variable After Update: 0003
The ID is: 3
Value of Some_Variable before Update: 9998
New Value of Some_Variable After Update: 9999
The ID is: 4
Value of Some_Variable before Update: 9999
New Value of Some_Variable After Update: 0001
The ID is: 5
Value of Some_Variable before Update: 0014
New Value of Some_Variable After Update: 0015
The ID is: 6
Value of Some_Variable before Update: 0903
New Value of Some_Variable After Update: 0904
The ID is: 7
Value of Some_Variable before Update: 2108
New Value of Some_Variable After Update: 2109
希望有帮助!
答案 2 :(得分:0)
您可以使用to_char和to_number
select TO_CHAR(to_number(my_column, '9999')+1) from dual
答案 3 :(得分:0)
tmp$ python get-pip.py
Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581) - skipping
Requirement already up-to-date: pip in /usr/lib/python2.7/dist-packages