Solved

Simple sql question

Posted on 2002-07-11
6
282 Views
Last Modified: 2012-06-21
Hi
I have a table in which a column is declared to be of type varchar.
Almost all the values for this column are numbers like 100, 120, 140, etc but some of the values are alpha characters like 'hundred', 'one twenty five' etc.

Now I want to delete all these alpha entries from the table.
Is there a simple sql query which can do this ?
thanks
0
Comment
Question by:AInamdar
6 Comments
 
LVL 2

Expert Comment

by:ddandekar
ID: 7148264
Assuming that the first character in varchar column c1 is numeric for the rows which you want to retain then following will delete the rows with nonnumeric first characters
delete from t1
where  substr(c1,1,1) not in
 ( '0','1','2','3','4','5','6','7','8','9')
/

This will have to be repeated for 2nd character , 3rd character etc.
0
 
LVL 3

Expert Comment

by:mnicoras
ID: 7148483
Hi,

not so sim[ple.

solution 1:

create or replace function is_char( iVal varchar2 ) return number is
mVal number;
mRet NUMBER;
begin
  begin
    mVal := iVal;
    mRet := 1;
  exception
    when others then
      mRet := 0;
  end;
return mRet;
end;

delete from <my_table> where is_char(<my_column>)=0;

solution 2:

delete from <my_table>
where
  instr(lower(<my_column>),'a')+
  instr(lower(<my_column>),'b')+
  instr(lower(<my_column>),'c')+
  instr(lower(<my_column>),'d')+
  instr(lower(<my_column>),'e')+
...
  instr(lower(<my_column>),'z') > 0
;


best regards,
Marius Nicoras
0
 

Expert Comment

by:justvineet
ID: 7148658
The following query will delete all the rows which have an alpha in its value.

delete from t1 where
ascii(substr(c1,1,1)) not between 48 and 57 OR
ascii(substr(c1,2,1)) not between 48 and 57 OR
ascii(substr(c1,3,1)) not between 48 and 57 OR
......
......
ascii(substr(c1,size_of_c1-1,1)) not between 48 and 57 OR
ascii(substr(c1,size_of_c1,1)) not between 48 and 57
/

Regards,
Vineet.
0
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.

 
LVL 3

Accepted Solution

by:
rkogelhe earned 30 total points
ID: 7148755
update mytable
set mynum = null
where
  translate(mynum,'X0123456789','X') is not null;

or

delete mytable
where
  translate(mynum,'X0123456789','X') is not null;
0
 

Author Comment

by:AInamdar
ID: 7149712
Query from rkogelhe  was short and worked for me .
thanks.
0
 
LVL 3

Expert Comment

by:patelgokul
ID: 9392149
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
accept rkogelhe's comment as an answer
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

patelgokul
EE Cleanup Volunteer
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.

Join & Write a Comment

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now