Unwanted Spaces when concatenating fields and text strings - sql

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

Related

Replace multiple repeating character to one

I have a varchar column, and each field contains a single word, but there are random number of pipe character before and after the word.
Something like this:
MyVarcharColumn
'|||Apple|||||'
'|||||Pear|||||'
'||Leaf|'
When I query the table, I wish to replace the multiple pipes to a single one, so the result would be like this:
MyVarcharColumn
'|Apple|'
'|Pear|'
'|Leaf|'
Cannot figure out how to solve it with REPLACE function, anybody knows?
vkp's method absolutely solves your issue. Another method that works, and also will work in a variety of other situations, is using a triple REPLACE()
SELECT REPLACE(REPLACE(REPLACE('|||Apple|||||', '|', '><'), '<>',''), '><','|')
This method will allow you to keep a delimiter between multiple strings where Mr. VPK's method will concat the strings and put a delim at the very beginning and the very end.
SELECT REPLACE(REPLACE(REPLACE('|||Apple|||||Banana||||||||||', '|', '><'), '<>',''), '><','|')
One way is to replace all the | with blanks and add a pipe character at the beginning and the end of string.
select '|'+replace(mycolumn,'|','')+'|' from tablename

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'

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

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');

Remove extra spaces if more than one space through vb.net Oracle query

Just looking for another way to remove any excess spaces(>1) throughout a query. I'm taking information for my Oracle query from an Excel spreadsheet so there's bound to be some excess zeroes due to user error and whatnot so that when I'm taking the information from the excel it will have 2 sometimes 3 extra spaces after some values so we won't get any records returned from Oracle when we query it through the application. I have tried Trim(ds.Tables.Item(0).Rows.Item(k).Item(i).ToString.ToUpper) but this isn't removing the extra spaces in some of the values. Is there some SQL statement I don't know of or perhaps another reason why Trim isn't working?
Edit: Was writing replace function incorrectly.
Original: string.Replace(" ", " ")
New: string = string.replace(" ", " ")
Use String.Replace(" ", String.Empty) instead. Trim only removes leading and trailing spaces.
On the Oracle side, you can use the REGEXP_REPLACE function, but you need to apply it to each column you want to remove the extra spaces from:
SELECT
REGEXP_REPLACE(myCol1, ' {2,}', ' ') AS myCleanCol1,
REGEXP_REPLACE(myCol2, ' {2,}', ' ') AS myCleanCol2,
FROM myTable
The example here looks for all occurrences of two or more spaces (that's the second parameter) and replaces them with a single space (the third parameter).
You can use REGEXP_REPLACE
Could be something like this:
regexp_replace(your_column, '\s{2,}', '')
Here is a sqlfiddle demo

Concatenate strings in Oracle SQL without a space in between?

I am trying to concatenate strings in oracle.
The following is my query:
insert into dummy values('c'||to_char(10000,'99999'));
The expected result is:
c10000
But the output I get is with a space in between 'c' and the value 10000:
c 10000
How to concat without spaces?
This is not an issue with the concatenation operator but with the function to_char(). Try instead:
to_char(10000,'FM99999')
I quote the manual here:
FM ..
Returns a value with no leading or trailing blanks.
There are two solutions:
Fill Mode ('FM') formatting prefix that suppresses the additional blank character prefix for the to_char number conversion. I suggest this one is preferred, because it is integrated with the to_char format and does not require an additional function call;
LTRIM of the returned value from the to_char number conversion.
The code below shows the results of both solutions:
Select concat('NTA', to_char(1,'FM0000000000000')),
concat('NTA', ltrim(to_char(1,'0000000000000'))),
concat('NTA', to_char(1,'0000000000000'))
from dual;
"CONCAT('NTA',TO_CHAR(1,'FM0000000000000'))": "NTA0000000000001"
"CONCAT('NTA',LTRIM(TO_CHAR(1,'0000000000000')))": "NTA0000000000001"
"CONCAT('NTA',TO_CHAR(1,'0000000000000'))": "NTA 0000000000001"