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

# Parsing a database field and returning occurrences

I need to be able to parse an Oracle database field and return the number of occurrences of CR/LF {chr(10) and/or chr(13)} in the field as a number.
You'll need to use the INSTR function to find the occurrences of CR/LF in the field. For example, let's use the following example:

SQL> create table t ( x varchar2(100) );
Table created.

SQL>
SQL> insert into t values ( 'Hello' || chr(10) || 'World' );

1 row created.

SQL> insert into t values ( 'Hello' || chr(13) || 'World' );

1 row created.

```SQL> select x, instr(x,chr(10))
2    from t
3  /

X           INSTR(X,CHR(10)) INSTR(X,CHR(13))
----------- ---------------- ----------------
Hello                      6     0
World

Hello
World                      0     6
```

See how the INSTR function returns the first occurrence of the chr(10) or chr(13). Now, you'd need to create a function that could be applied to the field to count the occurrences:

```create or replace function count_crlf ( p_str in varchar2 )
RETURN number
is
l_str_cr   long := p_str ;
l_str_lf   long := p_str ;
n          number;
ct_cr      number := 0;
ct_lf      number := 0;
begin
loop
exit when l_str_lf is null;
n := instr( l_str_lf, chr(10) );
if n > 0 then
ct_lf := ct_lf + 1 ;
end if ;
l_str_lf := substr( l_str_lf, n+1 );
end loop;

loop
exit when l_str_cr is null;
n := instr( l_str_cr, chr(13) );
if n > 0 then
ct_cr := ct_cr + 1 ;
end if ;
l_str_cr := substr( l_str_cr, n+1 );
end loop;

RETURN (ct_cr + ct_lf) ;
end;
/
```

I did each the counts for CR and LF separately... you could combine them into a single loop if you wanted or create two different functions if you need to have CR and LF totals returned separately rather than as a single total.

```Then you'd simply call the function and pass it the column value:
SQL> select count_crlf(x)
2    from t
3  /

COUNT_CRLF(X)
-------------
1
1

SQL> insert into t values ( 'Hello' || chr(13) || 'World' || chr(10) || chr(13) );

1 row created.

SQL> select count_crlf(x)
2    from t
3  /

COUNT_CRLF(X)
-------------
1
1
3
```

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### PlanetScale extends multi-cloud database as a service

Open source-based PlanetScaleDB adds support for Microsoft Azure, enabling a multi-cloud database as a service platform that ...

• ### Oracle NoSQL database comes to the cloud

Another Oracle database is coming to the cloud with the general availability of Oracle NoSQL Database Cloud Service as the demand...

• ### MariaDB SkySQL enables cloud-native database as a service

MariaDB brings back an old name for its cloud-native database-as-a-service platform. MariaDB CEO Michael Howard details how he's ...

• ### Importance of analytics clear amid COVID-19 crisis

With health organizations and business leaders alike trying to minimize the effects of COVID-19, analytics is playing a critical ...

• ### AI tools in analytics software key in fighting COVID-19

With data skewed by incomplete testing, augmented intelligence and machine learning tools are helping data scientists create ...

• ### Should your company hire a freelance data scientist?

Enterprises need to make the right hiring choices when it comes to supporting their data projects. Here are some reasons your ...

## SearchSAP

• ### SAP S/4HANA deployment options: Legacy ERP vs. cloud

Take this tour of the on-premises S/4HANA and its two cloud siblings and get expert advice on major differences in cost and ...

• ### Floating the possibilities of SAP S/4HANA Cloud migration

When weighing the advantages of abandoning legacy ERP systems for SAP S/4HANA Cloud, factors include implementation and support ...

## 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

• ### Eclipse launches Theia 1.0 as alternative to VS Code

The Eclipse Foundation's Theia framework for building desktop and cloud IDEs has the potential to be as big as the initial ...

• ### Developing a COVID-19 application? Design it the right way

Application developers with skills and an idea could design an app to combat the virus. Follow these insights to effectively ...

• ### How developers can volunteer during the COVID-19 pandemic

What resources can developers offer to go against COVID-19? Explore some promising developer volunteer and open source project ...

## SearchDataCenter

• ### Build a migration plan for enterprise edge infrastructure

Micro data centers are essential to edge computing use cases. Build an effective migration plan through site selection, ...

• ### Tech giants aim supercomputers, clouds at finding a COVID-19 cure

The new COVID-19 HPC consortium comprised of tech giants, national labs and academia are using supercomputers and clouds to speed...

• ### Top vendor SDDC certification programs for admins

If you decide to grow your knowledge of software-defined data center technologies, VMware, Nuage Networks and Microsoft offer ...

## SearchContentManagement

• ### Creating digital experiences key to driving exceptional CX

The way in which businesses build digital experiences is ever-evolving. It's a race to create easier-to-use customer interfaces ...

• ### Vendors offer free remote work technology for telecommuting

It's more than a marketing ploy: Cloud technology vendors, seeing their own issues enabling remote work, extend paid products to ...

• ### How businesses should deal with enterprise search issues

Enterprise search issues frequently complicate user experience with ECM systems. While users may face many problems, they also ...

## SearchHRSoftware

• ### How to create an employee journey map

This handy guide shows you how to build your own employee journey map, which is more important than ever as the coronavirus ...

• ### Remote team tech Sneek keeps co-workers in view

Sneek, a remote team platform, uses frequently updated photographs of remote workers to connect co-workers together.

• ### Best-case, worst-case pandemic scenarios for India service providers

India's IT and business support services will be tested during that country's pandemic lockdown. Many workers are now operating ...

Close