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'
Related
I am pretty new to SQL so I desperately need support with the following matter:
one of the columns of the table I am querying contains very long and various text values: so, I would like to limit the output to some kinds only.
I would like to get as output of the query the same content, limited to [a-z][A-Z][0-9], so for example:
Original table: Hello /% World, 2019
Result query: Hello World 2019
Does anybody have any idea?
Thank you very much
Assuming regexp_replace is supported,
regexp_replace(column, '[^a-zA-Z0-9]', '')
You didn't mention which SQL engine you are using, but most engines support the string function REPLACE which can use to substitute any character you want with an empty string to remove it. You will have to nest several of these to get rid of the different characters. Check the documentation of your database engine for the syntax.
For example:
SELECT REPLACE(REPLACE('Hello /% World, 2019, '/', ''), '%','')
In general, I would argue that this is not the job of the database engine, and you can do it in your presentation layer.
HTH
$string = 'Hello /% World, 2019';
Strip all characters but letters and numbers from a PHP string:
$res = preg_replace("/[^a-zA-Z0-9]/", "", $string);
Strip all characters but letters, numbers, and whitespace:
$res = preg_replace("/[^a-zA-Z0-9\s]/", "", $string);
BEST OPTION: Remove all special characters leaving no spaces
SELECT REGEXP_REPLACE(your_column, '[^0-9A-Za-z]', '') AS newfield
FROM tablename;
Remove many characters, leaving spaces for the same number of characters removed
SELECT TRANSLATE(columnname,'!##$%^&*()',
' ' ) AS newfield
FROM tablename;
If you just want to replace one character
SELECT REPLACE('columnname', '%', '') AS newfield
From tablename;
If you are looking for something in particular (You want to find the words "Hello World 2019")
Select ‘Hello World 2019' AS newfield
From tablename
WHERE columnname like ‘Hello%’ AND columnname like ‘%World%’ AND columnname like ‘%2019’;
Assuming Hello is always the beginning and 2019 is always at the end and you want all the fields. If you want other fields, you might want those as well; like,
Select TableID, columnname, ‘Hello World 2019' AS newfield
From tablename
WHERE columnname like ‘Hello%’ AND columnname like ‘%World%’ AND columnname like ‘%2019’;
When I run the following query in bigqeury I am getting no results.
SELECT COUNT(*) FROM raw.bicc_customers WHERE sub_type = "SUD"
I don't get any results
However, when I run
SELECT COUNT(*) FROM raw.bicc_customers WHERE sub_type LIKE "%SUD%"
I get results. The field that I'm looking for is SUD with no spaces, I don't understand why I need wildcards to find it. What am I missing?
EDIT:
So in the table the column is actually "SUD" - How would you find this without wildcards?
I couldn't add a comment, because I'm too new.
But try doing this query to help you determine what's wrong.
SELECT LENGTH(sub_type ) FROM raw.bicc_customers WHERE sub_type LIKE "%SUD%"
If you see that all the length values are greater than 3, that means you have some white space characters around it.
You can also do this to check:
SELECT LENGTH(sub_type ) FROM raw.bicc_customers WHERE LTRIM(RTRIM(sub_type)) = "SUD"
So in the table the column is actually "SUD" - How would you find this
without wildcards?
Option 1 - escaping double quotes - see Escaping special characters in strings for more details
SELECT COUNT(*)
FROM raw.bicc_customers
WHERE sub_type = "\"SUD\""
Option 2 - using single quotes
SELECT COUNT(*)
FROM raw.bicc_customers
WHERE sub_type = '"SUD"'
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
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');
It's pretty straightforward to strip out non-alphanumeric characters out from the search term, but how do you compare it to only the non-alphanumeric characters of values in the database?
For example, if I search for stack's, how can I get it to match both stacks and stack's?
What do I need to do to the what-do-i-do variable below to make the above happen?
SELECT * FROM table WHERE <what-do-i-do> ilike 'stacks'
One way to do this is with translate:
select *
from table
where translate(lower(WhatIDo), translate(lower(WhatIDo), 'abcdefghijklmnopqrstuvwxyz', ''), '') = 'stacks'
The inner translate finds all non-alpha characters. The outer then removes these from the string.
You can try to replace all instances of non-alphanumeric characters with the wildcard character ('%'). In your example:
SELECT * FROM table WHERE data like 'stack%s'