简体   繁体   中英

Oracle 18c XE- How to split string into numbers

I'm trying to split string into numbers. String looks like this:

POINT(-122.419956922531 37.7647100858191)

I'd like to remove 'Point' and paranthesis' and get only numbers. I tried this (code below). Even though it looks good in the table I keep on failing to convert results into numbers:

select 
   REGEXP_SUBSTR(geom_, '(\S*)' ) as longi,
    REGEXP_SUBSTR(geom_, '(\S*)[^()]',1,2) as atti    
from 
   (select 
      a.*,
      REGEXP_SUBSTR('POINT(-122.419956922531 37.7647100858191)', '\d.+') as geom_ from dual a);

Any tips on how to extract numbers from POINT(-122.419956922531 37.7647100858191) ?

SELECT REGEXP_SUBSTR(
         geom_,
         'POINT\(([+-]?\d+(\.\d+)?) ([+-]?\d+(\.\d+)?)\)',
         1,
         1,
         NULL,
         1
       ) AS longitude,
       REGEXP_SUBSTR(
         geom_,
         'POINT\(([+-]?\d+(\.\d+)?) ([+-]?\d+(\.\d+)?)\)',
         1,
         1,
         NULL,
         3
       ) AS latitude
FROM   your_table;

or:

SELECT REGEXP_SUBSTR( geom_, '[+-]?\d+(\.\d+)?', 1, 1 ) AS longitude,
       REGEXP_SUBSTR( geom_, '[+-]?\d+(\.\d+)?', 1, 2 ) AS latitude
FROM   your_table;

or with simple string functions:

SELECT SUBSTR( geom_, 7, INSTR( geom_, ' ' ) - 7 )
         AS longitude,
       SUBSTR( geom_, INSTR( geom_, ' ' ) + 1, LENGTH( geom_ ) - INSTR( geom_, ' ' ) - 1 )
         AS latitide
FROM   your_table

Which, for the sample data:

CREATE TABLE your_table ( geom_ ) AS
SELECT 'POINT(-122.419956922531 37.7647100858191)' FROM DUAL;

All of them output:

\nLONGITUDE | LATITUDE        \n:---------------- |  :--------------- \n-122.419956922531 |  37.7647100858191 \n

(Note: Wrap the output in TO_NUMBER if you want a number rather than a string containing a numeric value.)

db<>fiddle here


Update:

If your decimal separator is normally , then you can use TO_NUMBER and explicitly specify a . decimal separator as the third argument when using TO_NUMBER :

SELECT TO_NUMBER(
         REGEXP_SUBSTR( geom_, '[+-]?\d+(\.\d+)?', 1, 1 ),
         '999D9999999999999',
         'NLS_NUMERIC_CHARACTERS=''.,'''
       ) AS longitude,
       TO_NUMBER(
         REGEXP_SUBSTR( geom_, '[+-]?\d+(\.\d+)?', 1, 2 ),
         '999D9999999999999',
         'NLS_NUMERIC_CHARACTERS=''.,'''
       ) AS latitude
FROM ( select 'POINT(-122.419956922531 37.7647100858191)' as geom_ from dual );

db<>fiddle here

Here's a straightforward way to do it:

WITH cteString
       AS (SELECT 'POINT(-122.419956922531 37.7647100858191)' AS POINT_STRING FROM DUAL)
SELECT TO_NUMBER(REGEXP_SUBSTR(POINT_STRING, '[+-]?[0-9.]+', 1, 1)) AS LATITUDE,
       TO_NUMBER(REGEXP_SUBSTR(POINT_STRING, '[+-]?[0-9.]+', 1, 2)) AS LONGITUDE
  FROM cteString

The regular expression [+-]?[0-9.]+ accepts substrings consisting of the one or more digits 0-9 and the decimal point, optionally preceded by a sign character. It looks for the first such substring, converts the resulting substring to a NUMBER, and puts it into the result set as a field called LATITUDE. It then looks for the second such substring, converts it to NUMBER, and calls the LONGITUDE.

db<>fiddle here

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