Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Creating a sequence for a varchar in PL/SQL

I want to create a sequence for this varchar to abcd_ver2 in my PL/SQL. It would have been easier if it was a number instead of varchar. In that case, I could do

``` seq_num := seq_num + 1;
```
But what can I do when I want to change abcd_ver1 to abcd_ver2 (to increment the number in that varchar column by 1)?

Let's do this a brute force way. First, we'll need to find the number in the string, and then we'll need to increment it and add it back to the string. So we'll do this with something similar to the following PL/SQL block:

```DECLARE
my_string VARCHAR2(50);
string_portion VARCHAR2(50);
number_portion VARCHAR2(50);
the_number NUMBER;
string_location NUMBER;
BEGIN
/* define the string */
my_string := 'abcd_ver2';
/* find where the number is */
/* use the fact that INSTR returns zero */

string_location:=MIN(DECODE(INSTR(my_string,'0'),0,999,INSTR
(my_string,'0')),DECODE(INSTR(my_string,'1'),0,999,INSTR(my_string
('1')),....,DECODE(INSTR(my_string,'9'),0,999,INSTR(my_string,'9')));

/* now that we know where the number is, */
/* get the string and number portions */

string_portion:=SUBSTR(my_string,1,string_location-1);
number_portion:=SUBSTR(my_string,string_location);
/* Convert the number to NUMBER datatype */
/* and increment it */
the_number:=TO_NUMBER(number_portion);
the_number:=the_number+1;
/* Place the number back on the string */
my_string := string_portion || TO_CHAR(the_number);
END;
/
```
The above code can be modified for your specific needs. It relies on the fact that INSTR returns zero if the search is unsuccessful. If a zero is return, we use '999' instead, otherwise, we use the string position. We then return the minimum value of all string positions (or 999) to find the first occurence of a number in the main string. By using SUBSTR, we can pull apart the characters from the number. This lets us then increment the number and then concantenate it back with the characters. Don't forget to finish out the "MIN(DECODE" statement. I put "..." to indicate that you should finish for all numbers.

• The Best Oracle Web Links: tips, tutorials, scripts, and more.
• Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
• Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
• Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

## SearchDataManagement

• ### Who belongs on a high-performance data governance team?

Putting together a high-quality data governance team can be a challenge. Explore the necessary team members and best practices ...

• ### Soda launches cloud service to improve data observability

Data quality vendor Soda has had a busy 2021, building out new services and raising funding to help organizations identify and ...

• ### Bigeye raises \$17M Series A funding to boost data quality

The former Uber product manager and current CEO and co-founder of a startup outlines the challenges and opportunities of enabling...

• ### Insightsoftware acquisition of Izenda targets embedded BI

ERP vendor Insightsoftware acquired embedded analytics specialist Izenda on April 15, marking its second purchase of an embedded ...

• ### Data scientist shortage leaves organizations uncertain

Despite their desire to use data science in their decision-making process, some organizations can't find qualified data ...

• ### Air Force's data overhaul makes analytics a priority

With its data locked in legacy systems that didn't communicate with one another, in 2017 the Air Force started to overhaul of its...

## SearchSAP

• ### S/4HANA Cloud SaaS ERP: Buying team overview

SAP's multi-tenant SaaS ERP, S/4HANA Cloud, is a viable choice for companies that need ease in their infrastructure management. ...

• ### SAP forms financial services partnership with Dediq

SAP and financial industry investment firm Dediq are forming a new business unit to develop applications that help banks and ...

• ### Unpatched applications threaten SAP security

Cyberattacks are a significant threat to unpatched, unprotected SAP applications, according to a new threat intelligence report ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Incorporate diversity and inclusion in technology design

DEI in technology is about more than creating a diverse workplace. We talked to a few DEI professionals about how teams build ...

• ### Microsoft previews OpenJDK distro to the delight of devs

In a move meant to attract more Java developers to its Azure cloud and further support the Java community, Microsoft launched a ...

• ### Supreme Court ruling on Java APIs eases developer worries

Now that the Supreme Court has ruled for Google over Oracle in their high-stakes copyright battle over Java APIs, developers can ...

## SearchDataCenter

• ### Programmable processor technology for next-gen data centers

The right processing technology can benefit your data center. Learn about advancements in CPU technologies, recent vendor ...

• ### Data processing units accelerate infrastructure performance

DPUs often run on networking packets to move information in the data center, instead of supporting processing workflows. Get an ...

• ### New Intel Ice Lake processors boost performance, security

Intel launches third-generation Xeon Scalable processors that bolster security, accelerate common data center workloads by 46% on...

## SearchContentManagement

• ### OpenText releases Cloud Editions content services updates

OpenText CE 21.2 includes federated document compliance that extends to Microsoft Office 365, along with a revamped content ...

As the pandemic disrupts paper workflows, Adobe courts small business users with simple webforms, digital signatures and payments...

• ### Know when to use a headless CMS and when it's not worth it

Headless CMSes aren't a silver bullet for every circumstance. Evaluate three solid use cases for a headless CMS and three ...

## SearchHRSoftware

• ### 20 diversity and inclusion tools to power your DEI program

Diversity and inclusion tools shouldn't replace strategy, but these tools can help companies in multiple ways, from tracking ...

• ### DEI tools arrive at critical time for HR

Employers are being held accountable for meeting diversity, equity and inclusion goals, especially executives. HR vendors are ...

• ### Oracle Cloud HCM introduces low-code/no-code template tools

Oracle Cloud HCM has been upgraded to ease development of HR processes and improve employee experience, the firm said.

Close