简体   繁体   中英

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:

You could use like

SELECT * FROM POSTCODES_TBL t WHERE t.postcode like 'part1%part2'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM