简体   繁体   中英

Extracting data from column to a new column sql developer

I've imported data into SQL developer and I need to separate data from one column into a new column. For example I have:

Temp_Title
Congo (1995)
Nadja (1993)

I need to remove the year from the title into a new column named temp_year . I was told that I can use "Parse" but I'm not sure where to start. Any help is greatly appreciated.

You didn't specify database you use; also, you mentioned "SQL Developer" (designed by Oracle) but tagged the question with "plsqldeveloper" tag so - actual query might depend on certain info you didn't share with us.

Anyway, to get you started, here's an example (based on Oracle) which uses two options:

  • the first one uses traditional SUBSTR + INSTR combination
  • another one uses regular expressions

.

SQL> with test (temp_title) as
  2    (select 'Congo (1995)' from dual union all
  3     select 'Nadja (1993)' from dual union all
  4     select 'Back to the future (1985)' from dual
  5    )
  6  select
  7    substr(temp_title, 1, instr(temp_title, '(') - 1) title,
  8    substr(temp_title, instr(temp_title, '(') + 1, 4) year,
  9    --
 10    regexp_substr(temp_title, '(\w| )+') title2,
 11    rtrim(regexp_substr(temp_title, '\d+\)$'), ')') year2
 12  from test;

TITLE                YEAR             TITLE2               YEAR2
-------------------- ---------------- -------------------- -----
Congo                1995             Congo                1995
Nadja                1993             Nadja                1993
Back to the future   1985             Back to the future   1985

SQL>

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