How to extract numerical data from SQL result

独自空忆成欢 提交于 2019-12-06 01:46:53

I know this thread is old, and the OP doesn't need the answer, but I had to figure this out with a few hints from this and other threads. They all seem to be missing the exact answer.

The easy way to do this is to TRANSLATE all unneeded characters to a single character, then REPLACE that single character with an empty string.

DATA = 'Nowshak 7,485 m'

# removes all characters, leaving only numbers
REPLACE(TRANSLATE(TRIM(DATA), '_____________________________________________________________________________________________', ' abcdefghijklmnopqrstuvwzyaABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-_=+\|[]{};:",.<>/?'), '_', '')
 => '7485'

To break down the TRANSLATE command:

TRANSLATE( FIELD or String, <to characters>, <from characters> )

e.g.

DATA = 'Sample by John'

TRANSLATE(DATA, 'XYZ', 'abc')
 => a becomes X, b becomes Y, c becomes Z
 => 'SXmple Yy John'

** Note: I can't speak to performance or version compatibility. I'm on a 9.x version of DB2, and new to the technology. Hope this helps someone.

In Oracle:

SELECT TO_NUMBER(REGEXP_REPLACE(data, '[^0-9]', ''))
FROM a

In PostgreSQL:

SELECT CAST(REGEXP_REPLACE(data, '[^0-9]', '', 'g') AS INTEGER)
FROM a

In MS SQL Server and DB2, you'll need to create UDF's for regular expressions and query like this.

See links for more details.

Doing a quick search on line for DB2 the best inbuilt function I can find is Translate It lets you specify a list of characters you want to change to other characters. It's not ideal, but you can specify every character that you want to strip out, that is, every non numeric character available...

(Yes, that's a long list, a very long list, which is why I say it's not ideal)

TRANSLATE('data', 'abc...XYZ,./\<>?|[and so on]', ' ')

Alternatively you need to create a user defined function to search for the number. There are a few alternatives for that.

  1. Check each character one by one and keep it only if it's a numeric.

  2. If you know what precedes the number and what follows the number, you can search for those and keep what is in between...

To elaborate on Dems's suggeston, the approach I've used is a scalar user-defined function (UDF) that accepts an alphanumeric string and recursively iterates through the string (one byte per iteration) and suppresses the non-numeric characters from the output. The recursive expression will generate a row per iteration, but only the final row is kept and returned to the calling application.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!