Skip to content
Advertisement

How to Count How Many Zeros are within a column from the Right in Oracle SQL

I am currently going through a process of validating a column that holds reference numbers for each customer but they have to be a specific format to be counted as a genuine number.

I initially received around 80 csv files and uploaded this into a single master table after going through a process of making sure all tables have the same number of columns etc. (following is an example as I have over 90 columns).

The following is my create table query and I decided to keep everything as a varchar column bar the first ID column just to ensure there’s no issues with data being missed.

I then uploaded all my data and this gave me around 900k rows of data.

The specific column that I am interested in is the PA_Nmbr column. For this to be a genuine reference number it needs to be 13 digit long so I then wrote the following query to see what my record count looks like:

So this told me that around 70% of my data has the right length but the next check is to make sure when I have added the data into the database the numbers have not been rounded up. So for example if there was a genuine number 1234567899999 and this has been rounded up to 1234567890000.

SO where I am stuck is that I would like to write a query where I can do a count on how many 0s they are in the column from the right side.

So if I want to check quickly and do a count of how many records I have that start off with numbers i.e. 1234 and then all 0s I could quickly do that. That would tell me that there is a problem with the data itself.

I would really appreciate it if somebody could advice on how I could write this query.

Thanks in advance.

Advertisement

Answer

have a look at the SELECT LEN(REPLACE(‘YYNYNYYNNNYYNY’, ‘N’, ”)) method. A similar question has been asked and answered here: How to count instances of character in SQL Column

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement