Solved

how to check character in field

Posted on 2016-09-06
4
42 Views
Last Modified: 2016-10-23
I have table having maximum number values in character datatype. I want to find those values where no character is defined and replace it with null. eg. table having field receiptno which is character field. I have minimum records having values like a12455,c24353,21243,c34345.
I want to replace these values with null or 0 and change this field with number datatype.

How can I write case statement for this field in simple sql query?????
Please help.

receiptno
a12345
12234
122434
23255
d34355

I want output like

0
12345
122434
23255
0
0
Comment
Question by:newdeveloper india
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 168 total points (awarded by participants)
ID: 41785884
I do this using this function:
create or replace FUNCTION GetNumber(txt_IN IN varchar2)
RETURN NUMBER
IS
	res NUMBER (30);
BEGIN

BEGIN
  res := TO_NUMBER(txt_IN);
EXCEPTION
  WHEN VALUE_ERROR THEN res:=0;
END;
RETURN (res);
END GetNumber;

 

Open in new window

0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 166 total points (awarded by participants)
ID: 41786324
select case when regexp_like(receiptno,'[a-z]') then '0' else receiptno end receiptno
from yourtable;

If you want numbers to be returned, then just add to_number -
select to_number(case when regexp_like(receiptno,'[a-z]') then '0' else receiptno end) receiptno
from yourtable;
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 166 total points (awarded by participants)
ID: 41786373
rather than looking for specific "bad" characters to turn into 0, it might be more effective to look for anything that isn't a "good" character and then turn the string into 0

So, assuming 0-9 are your "good" characters, that might look something like this


select case when regexp_like(receiptno,'[^0-9]') then 0 else receiptno end
from yourtable
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle RAC 12c 8 88
oracle forms question 22 42
oracle sqlplus query delimiter 8 24
DECLARATION OF CURSOR IS INCOMPLETE OR MALFORMED 5 25
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question