Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:53:24+00:00 2026-05-14T08:53:24+00:00

I got a little problem i need a sql query that gives all rows

  • 0

I got a little problem

i need a sql query that gives all rows back that only contains 0 in it.

the column is defined as varchar2(6)
the values in the column looks like this:

Row      Value
1        0
2        00
3        00
4        100
5        bc00
6        000000
7        00000

my first solution would be like this:

Oracle:
substr('000000' || COLUMN_NAME, -6) = '000000'

SQL Server:
right('000000' + COLUMN_NAME, 6) = '000000'

is there an other way?
(it needs to work on both systems)

the output would be the row 1,2,3,6,7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:53:24+00:00Added an answer on May 14, 2026 at 8:53 am

    This is the simplest one:

    select * from tbl where replace(col,'0','') = ''
    

    If you will not make computed column for that expression, you can opt for function-based index(note: Oracle and Postgres already supports this; Sql Server as of version 2008, not yet) to make that performant:

    create index ix_tbl on tbl(replace(col,'0',''))
    

    [EDIT]

    I just keep the answer below for posterity, I tried to explain how to make the query use index from computed column.

    Use this:

    select * from tbl
    where ISNUMERIC(col) = 1 and cast(col as int) = 0
    

    For ISNUMERIC needs on Oracle, use this: http://www.oracle.com/technology/oramag/oracle/04-jul/o44asktom.html

    [EDIT]

    @Charles, re: computed column on Oracle:

    For RDBMSes that supports computed column but it doesn’t have persisted option, yes it will make function call for every row. If it supports persisted column, it won’t make function call, you have real column on the table which is precomputed from that function. Now, if the data could make the function raise an exception, there are two scenarios.

    First, if you didn’t specify persist, it will allow you to save the computed column (ALTER TABLE tbl ADD numeric_equivalent AS cast(col as int)) even if the result from the data will raise an exception, but you cannot unconditionally select that column, this will raise exception:

    select * from tbl
    

    this won’t raise exception:

    select * from tbl where is_col_numeric = 1
    

    this will:

    select * from tbl where numeric_equivalent = 0 and is_col_numeric = 1
    

    this won’t (Sql Server supports short-circuiting):

    select * from tbl where is_col_numeric = 1 and numeric_equivalent = 0 
    

    For reference, the is_col_numeric above was created using this:

    ALTER TABLE tbl ADD 
    is_col_numeric  AS isnumeric(col)       
    

    And this is is_col_numeric’s index:

    create index ix_is_col_numeric on tbl(is_col_numeric)
    

    Now for the second scenario, you put computed column with PERSISTED option on table that already has existing data(e.g. ‘ABXY’,’X1′,’ETC’) that raises exception when function/expression(e.g. cast) is applied to it, your RDBMS will not allow you to make a computed column. If your table has no data, it will allow you to put PERSISTED option, but afterwards when you attempt to insert data(e.g. insert into tbl(col) values('ABXY')) that raises an exception, your RDBMS will not allow you to save your data. Thereby only numeric text can be saved in your table, your PERSISTED computed column degenerate into a constraint check, albeit a full detoured one.

    For reference, here’s the persisted computed column sample:

    ALTER TABLE tbl ADD
    numeric_equivalent  AS cast(col as int) persisted
    

    Now, some of us might be tempted to not put PERSISTED option on computed column. This would be kind of self-defeating endeavor in terms of performance purposes, because you might not be able to create index on them later. When later you want to create index on the unpersisted computed column, and the table already has data ‘ABXY’, the database won’t allow you to create an index. Index creation need to obtain the value from column, and if that column raises an exception, it won’t allow you to create index on it.

    If we attempt to cheat a bit i.e. we immediately create an index on that unpersisted computed column upon table creation, the database will allow you to do that. But when we insert ‘ABXY’ to table later, it will not be saved, the database is automatically constructing index(es) after we insert data to the table. The index constructor receives exception instead of data, so it cannot make an index entry for the data we tried inserting, subsequently inserting data will not happen.

    So how can we attain index nirvana on computed column? First of all, we make sure that the computed column is PERSISTED, doing this will ensure that errors kicks-in immediately; if we don’t put PERSISTED option, anything that could raise exception will be deferred to index construction, just making things fail later. Bugs are easier to find when they happen sooner. After making the column persisted, put an index on it

    So if we have existing data ’00’,’01’, ‘2’, this will allow us to make persisted computed column. Now after that, if we insert ‘ABXY’, it will not be inserted, the database cannot persist anything from computed column that raised an exception. So we will just roll our own cast that doesn’t raise exception.

    To wit(just translate this to Oracle equivalent):

    create function cast_as_int(@n varchar(20)) returns int with schemabinding
    begin
    
        begin try
           return cast(@n as int);
        end try
        begin catch 
           return null;
        end catch
    
    end;
    

    Please do note that catching exception in UDF will not work yet in Sql Server, but Microsoft have plans to support that

    This is now our non-exception-raising persisted computed column:

    ALTER TABLE tbl ADD
    numeric_equivalent  AS cast_as_int(a) persisted
    

    Drop the existing index, then recreate it:

    create index ix_num_equiv on tbl(numeric_equivalent)    
    

    Now this query will become index-abiding-citizen, performant, and won’t raise exception even the order of conditions is reversed:

    select * from tbl where numeric_equivalent = 0 and is_col_numeric = 1
    

    To make it more performant, since the numeric_equivalent column doesn’t raise any more exceptions, we have no more use for is_col_numeric, so just use this:

    select * from tbl where numeric_equivalent = 0 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer These are steps I follow when I rename a project:… May 15, 2026 at 4:56 pm
  • Editorial Team
    Editorial Team added an answer Here's one simple way: int oldLen = stringArray[i].length(); stringArray[i] =… May 15, 2026 at 4:56 pm
  • Editorial Team
    Editorial Team added an answer Since the image's container is positioned absolutely, it is outside… May 15, 2026 at 4:56 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.