SQL select from list where white space has been added to end - sql

I'm trying to select some rows from an Oracle database like so:
select * from water_level where bore_id in ('85570', '112205','6011','SP068253');
This used to work fine but a recent update has meant that bore_id in water_level has had a bunch of whitespace added to the end for each row. So instead of '6011' it is now '6011 '. The number of space characters added to the end varies from 5 to 11.
Is there a way to edit my query to capture the bore_id in my list, taking account that trialling whitespace should be ignored?
I tried:
select * from water_level where bore_id in ('85570%', '112205%','6011%','SP068253%');
which returns more rows than I want, and
select * from water_level where bore_id in ('85570\s*', '112205\s*','6011\s*', 'SP068253\s*');
which didn't return anything?
Thanks
JP

You should RTRIM the WHERE clause
select * from water_level where RTRIM(bore_id) in ('85570', '112205','6011');
To add to that, RTRIM has an overload which you can pass a second parameter of what to trim, so if the trailing characters weren't spaces, you could remove them. For example if the data looked like 85570xxx, you could use:
select * from water_level where RTRIM(bore_id, 'x') IN ('85570','112205', '6011');

You could use the replace function to remove the spaces
select * from water_level where replace(bore_id, ' ', '') in ('85570', '112205', '6011', 'SP068253');
Although, a better option would be to remove the spaces from the data if they are not supposed to be there or create a view.

I'm guessing bore_id is VARCHAR or VARCHAR2. If it were CHAR, Oracle would use (SQL-standard) blank-padded comparison semantics, which regards 'foo' and 'foo ' as equivalent.
So, another approach is to force comparison as CHARs:
SELECT *
FROM water_level
WHERE CAST(bore_id AS CHAR(16)) IN ('85570', '112205', '6011', 'SP068253');

Related

Regex Postgres More than one dot

I need to return the fields that have more than one . in a specific column.
Now I have this query:
select *
from table
where column ~ '\.{2,}?';
But for some reason it returns nothing. If I use something like 'A{2,}?' it works. Apparently the problem is the dot.
It returns null since the dots are not next two each other. You have to consider the occurrences of the characters in the order of your regex meta characters. You could try this instead:
select *
from table
where column ~ '\.\d{3}\.';
Or instead of just focusing on the dot characters start parsing the string as a whole and consider the numbers as well:
where column ~ '^\d{3}\.\d{3}\.';
Why not just use like?
where column like '%.%.%'

Cut string after first occurrence of a character

I have strings like 'keepme:cutme' or 'string-without-separator' which should become respectively 'keepme' and 'string-without-separator'. Can this be done in PostgreSQL? I tried:
select substring('first:last' from '.+:')
But this leaves the : in and won't work if there is no : in the string.
Use split_part():
SELECT split_part('first:last', ':', 1) AS first_part
Returns the whole string if the delimiter is not there. And it's simple to get the 2nd or 3rd part etc.
Substantially faster than functions using regular expression matching. And since we have a fixed delimiter we don't need the magic of regular expressions.
Related:
Split comma separated column data into additional columns
regexp_replace() may be overload for what you need, but it also gives the additional benefit of regex. For instance, if strings use multiple delimiters.
Example use:
select regexp_replace( 'first:last', E':.*', '');
SQL Select to pick everything after the last occurrence of a character
select right('first:last', charindex(':', reverse('first:last')) - 1)

Oracle db query - data format issue

I'm wondering if there is a way to query the oracle db against formatted field value.
Example:
I have a table of postcodes stored in the format of "part1 part2". I want to be able to find a postcode either by searching it using the above format or "part1part2" format.
What I was thinking is to format the entered postcode by removing the spaces and then query the database like:
SELECT *
FROM POSTCODES_TBL t
WHERE t.postcode.**Format(remove spaces)** = 'part1part2'
The Format(remove spaces would convert the postcode from "part1 part2" to "part1part".
My question is, is it possible?
You can use regexp_replace
SELECT *
FROM POSTCODES_TBL t
WHERE regexp_replace(t.postcode,'\s', '') = 'part1part2'
This will remove any whitespace (space, tab, newlines etc)
or if you only want to get rid of spaces, replace will work just as well:
SELECT *
FROM POSTCODES_TBL t
WHERE replace(t.postcode,' ', '') = 'part1part2'
More details in the manual:
replace
regexp_replace
You could use like
SELECT *
FROM POSTCODES_TBL t
WHERE t.postcode like 'part1%part2'

Unwanted Spaces when concatenating fields and text strings

I'm putting the following in my SQL select statement to concatenate text strings (which are not fields in the database) with a couple database fields and I'm getting spaces where I try to use the to_char function to add leading zeros to a couple fields.
Running:
SELECT 'EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00') as ATS_NBR
Yields:
EP. 0017092. 01
How to I eliminate the unnecessary spaces?
Try using replace function:
SELECT replace(('EP.'||TO_CHAR(PWROTPR_TRANSX_NBR,'0000000')||'.'||TO_CHAR(PWROTPR_SUBMIT_COUNTER,'00')), ' ', '') as ATS_NBR

replace two characters in one cell

I am using this query to replace one character in a cell
select replace(id,',','')id from table
But I want to replace two characters in a cell.
If the cell is having this data (1,3.1), and I want it to look like this (131).
How can I replace two different characters in one cell?
Use TRANSLATE instead of REPLACE(). It replaces each occurrence of a character in the first pattern with its matched character in the second. To remove characters, simply leave cut short the replacement string:
select translate(id, '1,.', '1') id from table
Note that the second string cannot be null. Hence the need to include 1 (or some other character) in both strings.
Find out more.
Obviously the more characters you need to convert/remove the more attractive TRANSLATE() becomes. The main use for REPLACE is changing patterns (such as words) rather than individual characters.
Can use
select replace(translate(id,',.',' '),' ','') from table;
or
select regexp_replace('1,3.1','[,.]','') from dual;
or
select replace(replace(id,',',''),'.','') from table;
Call the replace again.
select replace(replace(id,',',''), '.','') id from table
Do this:
select REPLACE(REPLACE(id,',',''),'.','')
Or use a regular expression:
select regexp_replace(id, '[.,]', '') id from table
Find out more